Hello,

I wrote the following code to create dynamic controls-textboxes and labels.

ctlRow += 1
        ctlRowLocation += 25

        Dim new_ForceLabel As New Label
        new_ForceLabel.Text = "Force" + Convert.ToString(ctlRow - 1)
        new_ForceLabel.Location = New Point(txtForceLabel.Location.X + 2, ctlRowLocation)
        new_ForceLabel.Height = 25
        new_ForceLabel.Width = 50

        Dim new_ForceInput As New TextBox
        new_ForceInput.Name = "Force" + Convert.ToString(ctlRow - 1)
        new_ForceInput.Location = New Point(txtForce.Location.X + 2, ctlRowLocation)
        new_ForceInput.Height = 25
        new_ForceInput.Width = 75

        Dim new_DirectionLabel As New Label
        new_DirectionLabel.Text = "Direction" + Convert.ToString(ctlRow - 1)
        new_DirectionLabel.Location = New Point(txtDirectionLabel.Location.X + 2, ctlRowLocation)
        new_DirectionLabel.Height = 25
        new_DirectionLabel.Width = 75

        Dim new_DirectionInput As New TextBox
        new_DirectionInput.Name = "Direction" + Convert.ToString(ctlRow - 1)
        new_DirectionInput.Location = New Point(txtDirection.Location.X + 2, ctlRowLocation)
        new_DirectionInput.Height = 25
        new_DirectionInput.Width = 75

        Me.GroupBox1.Controls.Add(new_ForceLabel)
        Me.GroupBox1.Controls.Add(new_ForceInput)

        Me.GroupBox2.Controls.Add(new_DirectionLabel)
        Me.GroupBox2.Controls.Add(new_DirectionInput)


        GroupBox1.Height = ((ctlRow * 25) + 5)
        GroupBox2.Height = ((ctlRow * 25) + 5)

How can I access the data entered into these textboxes. Please help. I can't seem to figure out a way to do so.

Thanks in advance.

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)
Comments
Perfectly answers my question

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)

Thank you for your time and reply Reverend Jim. I greatly appreciate it.
I skimmed through your code (I didn't have time to implement it, I'll try it for sure when I get back home), but for now could you explain to me in moderate detail as to what you have done here. I have a few doubts such as your code already assumes that there are going to be 4 textboxes (0,1,2,3) but in my application this is not the case. I can have more than that too.

Thanks again for your help. Kindly just explain certain important aspects of the code you provided.

Thank You

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?

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?

Thanks a lot for the explanation. I have understood and successfully implemented what you explained in my program. Your reply indeed helped solve my problems.

Thanks a lot Reverend Jim.

:)

This question has already been answered. Start a new discussion instead.