• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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
Most Helpful Member
Copy & paste, then just change the number in two places. Tedious but not that bad.
 

Pommie

Well-Known Member
Most Helpful 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

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        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
                AddHandler newCtrl.Click, AddressOf TextBox_clicked
                boxes(i) = newCtrl
                Me.Controls.Add(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.
 

Pommie

Well-Known Member
Most Helpful 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
Most Helpful 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

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        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
                AddHandler newCtrl.Click, AddressOf TextBox_clicked
                boxes(i) = newCtrl
                Me.Controls.Add(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

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        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
                AddHandler newCtrl.Click, AddressOf TextBox_clicked
                boxes(i) = newCtrl
                Me.Controls.Add(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?
 

Pommie

Well-Known Member
Most Helpful Member
Unfortunately, Microsoft seem to think that processing speed, memory and disk space are limitless. As far as I'm aware, you have to install all of the Visual Studio suite.

Mike.
Edit, Does the above code make sense to you? Are you able to use it?
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top