I have a program that allows a user to create a number of Textboxes that they specify. I would like to know how to get the information from each of those Textboxes in a different Sub.

Here is code on creating them:

Dim txtLetter(x) As TextBox
                txtLetter(x) = New TextBox
                Me.Controls.AddRange(txtLetter)
                txtLetter(x).Location = New System.Drawing.Point(121, txtLocation)
                txtLetter(x).Size = New System.Drawing.Size(30, 20)
                txtLetter(x).MaxLength = 1
                FormSize = txtLetter(x).Location.Y + 55

I have that in one Sub then how do I get txtLetter1 in another sub?

Assign each of the new textbox's a name when creating them. Then in your sub, loop thru the textbox controls on the form until you find the one with the name you want.

Dim txtLetter(0) As TextBox
        txtLetter(0) = New TextBox
        txtLetter(0).Name = "txtLetter1"
        Me.Controls.AddRange(txtLetter)
        txtLetter(0).Location = New System.Drawing.Point(121, 0)
        txtLetter(0).Size = New System.Drawing.Size(30, 20)
        txtLetter(0).MaxLength = 1
For Each obj As Control In Me.Controls
            If TypeOf (obj) Is TextBox And obj.Name = "txtLetter1" Then
                MsgBox(obj.Text)
            End If
        Next

Edited 7 Years Ago by TomW: n/a

I can't call the name I give it or get a value from it. If I name it "TXTBOX" then try to get a value from it, it says its not declared.

No you cant call it by name, thats why Im using the loop to itterate thru each textbox's and compare names in an IF statement.

instead of looping you can use the find function like Me.Controls.Find("txtLetter1", True)(0) this ofcourse only works if you assign a name to the control on creation and the control exist.

instead of looping you can use the find function like Me.Controls.Find("txtLetter1", True)(0) this ofcourse only works if you assign a name to the control on creation and the control exist.

I've just tried that, but how do I get a value from it? I have:

For x = 0 To Boxes
                Me.Controls.Find("txtLetter" & x, True)
            Next

but how do I get a value out of the text box?

Also, I have tried:

Dim x As Integer
            For Each obj In Me.Controls
                If TypeOf obj Is TextBox And obj.Name = "txtLetter" & x Then
                    MessageBox.Show(obj.Text)
                    x += 1
                End If
            Next

but it doesn't show a message box or do anything.

Edited 7 Years Ago by Demon4231: n/a

This article has been dead for over six months. Start a new discussion instead.