# VB Textbox Event

Status
Not open for further replies.

#### Suraj143

##### Active Member
I have over 100 textboxes.I want to write its click events in a short mannar.
I have to write the below code for 100 times.
Code:
.
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click

End Sub
Is thee any shorter way of doing?
I use visual Basic 2015

#### Suraj143

##### Active Member
HI, I mean like this.
I have a variable called "Colour = Red".
If I click "Textbox1" it will Load the colour "Red" to the "Textbox1".It is working.

The problem is in "Form_Load" I have to write the 100 events for 100 Textboxes....!!!

Code:
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
End Sub

#### JonSea

##### Well-Known Member
Copy & paste, then just change the number in two places. Tedious but not that bad.

#### Pommie

##### Well-Known Member
Here's some code that places 0 to 99 in 100 text boxes and when you click one it displays the number.
Code:
Public Class Form1
Private boxes(100) As TextBox

For i = 0 To 9
For j = 0 To 9
Dim newCtrl As TextBox = New TextBox
newCtrl.Size = New Drawing.Size(50, 50)
newCtrl.Location = New Point(10 + 50 * j, 10 + 50 * i)
newCtrl.Text = i * 10 + j
boxes(i) = newCtrl
Next
Next
End Sub

Private Sub TextBox_clicked(sender As System.Object, e As System.EventArgs)
Dim box As TextBox = DirectCast(sender, TextBox)
MsgBox("You clicked the box containing " & box.Text)
End Sub
End Class
Mike.

#### Suraj143

##### Active Member
Hi Mike Thanks for the nice piece of code.

I got this error when I building....

#### Attachments

• 133.8 KB Views: 6

#### Pommie

##### Well-Known Member
It looks like you're using a later version than me (I'm on 2010). I'll look tomorrow to try to work out how to do it in later versions. It's 2AM here and I definitely need beauty sleep even though it doesn't seem to be working.

Mike.

#### Suraj143

##### Active Member
It looks like you're using a later version than me (I'm on 2010). I'll look tomorrow to try to work out how to do it in later versions. It's 2AM here and I definitely need beauty sleep even though it doesn't seem to be working.

Mike.
Many Thanks.

#### Pommie

##### Well-Known Member
Just installed VS2017 and the only change needed was to the form1_Load line which is auto generated.
Code:
Public Class Form1
Private boxes(100) As TextBox

For i = 0 To 9
For j = 0 To 9
Dim newCtrl As TextBox = New TextBox
newCtrl.Size = New Drawing.Size(50, 50)
newCtrl.Location = New Point(10 + 50 * j, 10 + 50 * i)
newCtrl.Text = i * 10 + j
boxes(i) = newCtrl
Next
Next
End Sub

Private Sub TextBox_clicked(sender As System.Object, e As System.EventArgs)
Dim box As TextBox = DirectCast(sender, TextBox)
MsgBox("You clicked the box containing " & box.Text)
End Sub
End Class
Mike.

#### Suraj143

##### Active Member
Just installed VS2017 and the only change needed was to the form1_Load line which is auto generated.
Code:
Public Class Form1
Private boxes(100) As TextBox

For i = 0 To 9
For j = 0 To 9
Dim newCtrl As TextBox = New TextBox
newCtrl.Size = New Drawing.Size(50, 50)
newCtrl.Location = New Point(10 + 50 * j, 10 + 50 * i)
newCtrl.Text = i * 10 + j
boxes(i) = newCtrl
Next
Next
End Sub

Private Sub TextBox_clicked(sender As System.Object, e As System.EventArgs)
Dim box As TextBox = DirectCast(sender, TextBox)
MsgBox("You clicked the box containing " & box.Text)
End Sub
End Class
Mike.
What a piece of code.It worked nicely.Many thanks for the support as always.....

By the way, The visual studio 2015 is really really slow IDE to work.Also I cannot install only VB, By default it is installed C#,F+ etc... I don't want them either and no way to uninstall them.

Do you have the same thing in Visual Studio 2017? Can you keep only VB in VS2017?