Array in command Button VBA - vba

I'm doing a code where, when i click button 1, it will load the array form the range B8:C17, and when I click button 2, it should print the array in range E8:F17.
Private Sub CommandButton1_Click()
arr = Range("B8:C17")
Range("B8:C17") = Clear
End Sub
Private Sub CommandButton2_Click()
Range("E8:F17") = arr
End Sub
Private Sub UserForm_Click()
Dim arr As Variant
End Sub
The button 1 works fine but the button 2 does not. What did I do wrong here ?

In your code = Clear will yield an error, it should be used as method like this:
Option Explicit
Public arr
Private Sub CommandButton1_Click()
arr = Range("B8:C17")
Range("B8:C17").Clear
End Sub
Private Sub CommandButton2_Click()
Range("E8:F17") = arr
End Sub
'Private Sub UserForm_Click()
'
'End Sub

Related

How can I cancel a thread?

I do something in a thread. But sometimes I don't want to wait till all pings are finished.
How can I cancel a thread?
Can you show me please the code?
Private Sub Start_Button_Click(sender As Object, e As EventArgs) Handles Start_Button.Click
DoSomething()
End Sub
Private Sub Cancel_Button_Click(sender As Object, e As EventArgs) Handles Cancel_Button.Click
THRD.Cancel '<-- Thread cancel!??!???
End Sub
Sub DoSomething()
Dim THRD As New Thread(Sub()
Dim IPArea As String = "192.168.1."
Dim LastIP As Integer
For LastIP = 0 To 255
Dim TestIP As String = IPArea & CStr(LastIP)
If My.Computer.Network.Ping(TestIP, 10) Then
ListBox1.Items.Add(TestIP)
End If
Next
End Sub)
THRD.IsBackground = True
THRD.Start()
End Sub
Here is my working solution and this solution is only to show how to move the THRD as a form level variable to allow stopping it when clicking the cancel button. I added some validations to prevent exceptions.
Public Class Form1
Private THRD As Threading.Thread
Private Sub Start_Button_Click(sender As Object, e As EventArgs) Handles Start_Button.Click
DoSomething()
End Sub
Private Sub Cancel_Button_Click(sender As Object, e As EventArgs) Handles Cancel_Button.Click
If THRD IsNot Nothing AndAlso THRD.IsAlive Then
THRD.Abort() '<-- Thread cancel!??!???
THRD = Nothing
AddToList("Stopped.")
Else
AddToList("Thread not running.")
End If
End Sub
Sub DoSomething()
If THRD IsNot Nothing AndAlso THRD.IsAlive Then
AddToList("Still working...")
Exit Sub
End If
THRD = New Threading.Thread(Sub()
Dim IPArea As String = "192.168.1."
Dim LastIP As Integer
For LastIP = 0 To 255
Dim TestIP As String = IPArea & CStr(LastIP)
If My.Computer.Network.Ping(TestIP, 10) Then
AddToList(TestIP)
End If
Next
AddToList("Done")
End Sub)
THRD.IsBackground = True
THRD.Start()
End Sub
''' <summary>
''' Thead-safe add value to list.
''' </summary>
''' <param name="value">The value.</param>
Private Sub AddToList(value As String)
If ListBox1.InvokeRequired Then
ListBox1.Invoke(Sub() ListBox1.Items.Add(value))
Else
ListBox1.Items.Add(value)
End If
End Sub
End Class

Cant change active textbox and set focus after mouse click on it

After second click on textboxes i cant write nothing after click on button "1". Setfocus is working only at first click (img).
Can You help?
Greetings
Private activeTextbox As Control
Private Sub Textbox_sr_Enter()
Set activeTextbox = Controls("Textbox_sr")
End Sub
Private Sub CommandButton1_Click()
If Not activeTextbox Is Nothing Then
activeTextbox.Text = activeTextbox.Text & "1"
activeTextbox.SetFocus
End If
End Sub
Private Sub TextBox1_Enter()
Set activeTextbox = Controls("TextBox1")
End Sub
Private Sub TextBox2_Enter()
Set activeTextbox = Controls("TextBox2")
End Sub
Private Sub TextBox3_Enter()
Set activeTextbox = Controls("TextBox3")
End Sub

How change Compare Multiple Textbox

I have 2 codes to change the color to Textbox, and unfortunately none of them work. What's wrong here? Why the code is not good. Every time I try the code, not worked, The first code changes the color to Textbox if there is a value between 1 and 7, and the second changes the value in ascending order from the lowest to the highest and assigns a corresponding color. Please tell me if these 2 codes are written correctly, or there is a write error.
Code 1: Image: http://www.imagebam.com/image/5ac5ee1073004874
Public Class TextBoxColors
Private ColorTable As Dictionary(Of String, Color) = New Dictionary(Of String, Color)()
Public Sub New()
ColorTable.Add("1", Color.Red)
ColorTable.Add("2", Color.Aqua)
ColorTable.Add("3", Color.Chocolate)
ColorTable.Add("4", Color.BlanchedAlmond)
ColorTable.Add("5", Color.BurlyWood)
ColorTable.Add("6", Color.BlueViolet)
ColorTable.Add("7", Color.DarkBlue)
End Sub
Public Function GetColor(ColorMap As String) As Color
Return If(ColorTable.Keys.Contains(ColorMap), ColorTable(ColorMap), Color.White)
End Function
End Class
Private txtColor As TextBoxColors = New TextBoxColors()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each txtDraw As TextBox In Me.Controls.OfType(Of TextBox).Where(Function(txt) txt.Name.StartsWith("txtDraw"))
AddHandler txtDraw.TextChanged,
Sub()
If Not String.IsNullOrEmpty(txtDraw.Text) Then
txtDraw.BackColor = txtColor.GetColor(txtDraw.Text)
End If
End Sub
Next
End Sub
Code 2: This 2nd code must be changed based on my text box, which starts with SumtxtDraw
- Image: http://www.imagebam.com/image/92a4091073004904
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillColorList()
End Sub
Private Sub ColorTextBoxes()
FillTextBoxList(16, 23)
Dim SortedList As List(Of TextBox) = SortList()
Dim index As Integer
For Each txt As TextBox In SortedList
txt.BackColor = lstColor(index)
index += 1
Next
End Sub
Private Sub FillColorList()
lstColor.Add(Color.Red) 'for lowest number
lstColor.Add(Color.BlanchedAlmond)
lstColor.Add(Color.PaleGreen)
lstColor.Add(Color.Chartreuse)
lstColor.Add(Color.CadetBlue)
lstColor.Add(Color.Orange)
lstColor.Add(Color.DarkMagenta)
lstColor.Add(Color.Violet) 'for highest number
End Sub
Private Sub FillTextBoxList(StartNumber As Integer, EndNumber As Integer)
lstTextBox.Clear()
For suffix = StartNumber To EndNumber
lstTextBox.Add(DirectCast(Controls("TextBox" & suffix.ToString), TextBox))
Next
End Sub
Private Function SortList() As List(Of TextBox)
Dim orderedList = From txt In lstTextBox Order By CInt(txt.Text) Descending Select txt '$"{scorer.Score} - {scorer.Name}"
Dim SortedList As List(Of TextBox) = orderedList.ToList
Return SortedList
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ColorTextBoxes()
End Sub

Checklistbox Multiple Selection

I have been working on a application that uses a Checkedlistbox so I can allow the user to select multiple boxes.
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
For Each item As Object In Me.CheckedListBox1.CheckedItems
Dim text As String = Me.CheckedListBox1.GetItemText(item)
Next
If text = "Line 1" Then
CreateLine1()
End If
If text = "Line 2" Then
CreateLine2()
End If
If text = "Line 3" Then
CreateLine3()
End If
If Text = "Line 4" Then
CreateLine4()
End If
If Text = "Line 5" Then
CreateLine5()
End If
It goes all the way to "Line 10". When the app runs it use cmd.exe to connect to telnet and send commands. If I have Line 1 and Line 2 selected Line 1 has no problems, but when Line 2 run it opens a cmd does, nothing for a few seconds, open another cmd, and run just the commands while not connected to the telnet. Several more widows open afterwords and the four or fifth window connected to telnet.
How can I make it so if one line if selected after it has run telnet it separates out that line as "Has been ran" before going to the next line to avoid my problem.
Addition info:
This app has a select-all and deselect-all buttons so I can not have anything that will interfere with them.
I have try using socket to replace cmd.exe.....it did not go so well and I will pass on it.
Each sub the lines go to it basically the same except to the IP address and a few commands.
I hope the original code you posted isn't actually what you're using...it doesn't seem quite right.
Perhaps something like this might be more useful:
Imports System.Reflection
Public Class Form1
Private Methods As New List(Of MethodInfo)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim MI As MethodInfo
For i As Integer = 1 To 10
mi = Me.GetType.GetMethod("CreateLine" & i, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public)
If Not IsNothing(MI) Then
Methods.Add(MI)
End If
Next
End Sub
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
For Each Index As Integer In Me.CheckedListBox1.CheckedIndices
Methods(Index).Invoke(Me, Nothing)
Next
End Sub
Private Sub CreateLine1()
Debug.Print("CreateLine1()")
End Sub
Private Sub CreateLine2()
Debug.Print("CreateLine2()")
End Sub
Private Sub CreateLine3()
Debug.Print("CreateLine3()")
End Sub
Private Sub CreateLine4()
Debug.Print("CreateLine4()")
End Sub
Private Sub CreateLine5()
Debug.Print("CreateLine5()")
End Sub
Private Sub CreateLine6()
Debug.Print("CreateLine6()")
End Sub
Private Sub CreateLine7()
Debug.Print("CreateLine7()")
End Sub
Private Sub CreateLine8()
Debug.Print("CreateLine8()")
End Sub
Private Sub CreateLine9()
Debug.Print("CreateLine9()")
End Sub
Private Sub CreateLine10()
Debug.Print("CreateLine10()")
End Sub
End Class
There are lots of other ways to do this as well...

vb 2012 module (F as Form)

my module code is
Module print
Sub Pad (F as form)
F.Textbox1.text = "testing"
End sub
End module
is there a way to use the "print.pad" to my 3 form (FrmNew,FrmOld,FrmAdmin) like this
Public Class FrmNew
Private Sub FrmNew_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Print.Pad(Me)
End Sub
End Class
Because in VBA it work.
VBA MODULE code (Module name Printt)
Sub Pad(f As Form)
f.Text0 = "testing"
End Sub
VBA FORM Code (Form name Form1)
Private Sub Form_Load()
Call Printt.Pad(Form_Form1)
End Sub
or any way around to do it. tnx in advance...
Module print
Public Sub Pad (ByVal txtControl As TextBox)
txtControl.Text = "testing"
End sub
End module
and call like
Public Class FrmNew
Private Sub FrmNew_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Print.Pad(textbox1)
End Sub
End Class
here textbox1 is the control in your FrmNew,In this you want show textbox1.text = "testing"