Hi,

I currently have a form with about 20 textboxes on, labeled Textbox1, Textbox2 and so on...

I would like to know how to get the data from any one of these textboxes from an integer.

Dim text as String
text = "Textbox" & RandomInteger & ".Text"
msgbox(text)

Anytime I do this, I just get the textbox name eg, "Textbox10.Text"

I am using Visual Studio 2010,

Thanks in advance

The easiest way would be trying get the control by its name and return Textbox object.

Public Function GetTextBoxByItName(ByVal name As String) As TextBox
        For Each value As Control In Me.Controls
            If value.Name.ToUpper = name.ToUpper Then
                Return value
            End If
        Next

        Return Nothing
End Function

To use this function

Dim text as String
text = GetTextBoxByItName("Textbox" & RandomNumber).Text
msgbox(text)

There is better to handle multiple textbox, by create array of textbox and then add them into Form.Controls

Thank you for you quick response, invisal

As far as I can see, your code works very well. I would like the function to run more than once, eg, get the data from each of the 20 textboxes

Any idea on how to do this?

If you have all your textbox name, Textbox1, Textbox2, Textbox3 ..., Textbox20, then just put it into a loop.

For i As Integer = 1 To 20
    Msgbox(GetTextBoxByItName("Textbox" & i.ToString()).Text)
Next

Like I posted earlier, this way of handling multiple textbox is not the best way. The better way would try to group those textbox into array.

The only problem is that I get an error: Object reference not set to an instance of an object

Would it be better to attempt an array at this point?

Object reference not set to an instance of an object occurs when my function cannot find the given textbox name. Probally, you didn't have all the textbox name in sequence such as Textbox1 to Textbox20.

Another solution is to have array of textbox reference.

Dim TextboxList(19) As TextBox

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                Handles MyBase.Load
        TextboxList(0) = Textbox1
        TextboxList(1) = Textbox2
        TextboxList(2) = Textbox3
        TextboxList(3) = Textbox4
        TextboxList(4) = Textbox5
        TextboxList(5) = Textbox6
        TextboxList(6) = Textbox7
        TextboxList(7) = Textbox8
        TextboxList(8) = Textbox9
        TextboxList(9) = Textbox10
        TextboxList(10) = Textbox11
        TextboxList(11) = Textbox12
        TextboxList(12) = Textbox13
        TextboxList(13) = Textbox14
        TextboxList(14) = Textbox15
        TextboxList(15) = Textbox16
        TextboxList(16) = Textbox17
        TextboxList(17) = Textbox18
        TextboxList(18) = Textbox19
        TextboxList(19) = Textbox20
    End Sub

To access any textbox from given number

Msgbox(TextboxList(0).Text)
Msgbox(TextboxList(1).Text)

You can loop through all the textbox by

For i As Integer = 0 To TextboxList.Count - 1
   Msgbox(TextboxList(i).Text)
Next
Member Avatar for Unhnd_Exception

Heres another idea.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = "Hello"

        'It doesn't matter if TextBox1 is nested within a million controls
        'it will still be found with GetControlText.
        MsgBox(GetControlText(Me, "Textbox1"))
    End Sub


    Private Function GetControlText(ByVal parentContainer As Control, ByVal controlName As String) As String

        Dim StackOfControls As New Stack(Of Control)
        Dim Child As Control

        'Start searching the parent control.
        StackOfControls.Push(parentContainer)

        'And keep searching until everything single child control
        'is checked or until the control name is found.
        Do While StackOfControls.Count > 0

            'Get the next control in the stack and check its name.
            Child = StackOfControls.Pop

            'If this control's name is = to the name passed in then return its text
            If Child.Name.ToLower = controlName.ToLower Then
                Return Child.Text
            End If

            'This wasn't the control being looked for.  If it has
            'controls then add it's controls to the stack so 
            'they can be searched.
            For Each ChildOfAChild In Child.Controls
                StackOfControls.Push(ChildOfAChild)
            Next
        Loop

        'The name was not in the parent.  Return an empty string or
        'throw an error.
        Return String.Empty
    End Function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.