You could create an array of TextBox references and populate it as you create the controls as in
Public Class Form1
Private MyText(3) As TextBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 3
Dim box As New TextBox
box.Location = New System.Drawing.Point(200, 10 + i * 25)
box.Size = New System.Drawing.Size(50, 23)
AddHandler box.TextChanged, AddressOf MyTextTextChanged
MyText(i) = box
Me.Controls.Add(box)
Next
End Sub
Private Sub MyTextTextChanged(sender As System.Object, e As System.EventArgs)
'add your code here
End Sub
End Class
Then you can refer to the properties (such as Text) in fht eollowing way
MsgBox(MyText(2).Text)
Reverend Jim
Posting Shark
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
When you create a TextBox in the IDE and name it (default is TextBox1) you get an object with that name and you access the methods and parameters with dot notation like TextBox1.Text. Consider the name (or variable) TextBox1 to be just a reference (pointer) to the actual object. Just like if you have an integer variable, x, with the value 5. X is not actually 5, but a reference to some location in memory which contains the value, 5.
If you need to refer to a bunch of text boxes conveniently (like in a loop) then instead of creating individual object references like TextBox1, TextBox2, etc, you can create an array of references as in
Private boxes(5) As TextBox
You haven't actually created the TextBoxes yet. All you have done is create a place to keep references (pointers) to them. Just like there is a difference between
Dim box as TextBox 'declares a TextBox variable
Dim box as New TextBox 'creates a TextBox and saves a reference to it in box
To use the dot notation, the left part of the expression must be a reference to an object. For a single object (like box) you just give the variable name. For an array of objects you have to narrow it down to a single instance. Thus you access a single object by
boxes(i).Text
Just like in an array of integers you can't say
Dim x(5) as Integer
x = 5
You have to give a specific instance as in
x(i) = 5
Am I going into too much detail?
Reverend Jim
Posting Shark
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159