Hey all,

I'm trying to do a project which involves reading the text from a number of textboxes and putting it into a string.

I'm just wondering if there's an easier way to it other than:

CreateString += TextBox1.Text + TextBox2.Text + TextBox3.Text... (etc)

I have thought about creating a For Next loop which would go something like:

For i As Integer = 0 To intNoTextBoxes
CreateString += TextBox(i).Text
Next

But this really doesn't work. I also found something saying that something similar to this would work:

For i As Integer = 0 To intNoTextBoxes
CreateString += Me.Controls.Item("TextBox" & i.ToString).Text
Next

But that didn't work either. Any suggestions would be greatly appreciated :)

Thanks in advance

See if this helps.

Private sTemp As String = Nothing '// TEMP String.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        sTemp = "" '// clear for new input.
        For Each myCoolTextBox In New TextBox() {TextBox1, TextBox2, TextBox3} '// your TextBoxes.
            sTemp &= myCoolTextBox.Text '// add to Strings using "&" and add.#'s up using "+", less confusing. :)
        Next
        MsgBox(sTemp) '// result.
    End Sub

AndAlso :

Private sTemp As String = Nothing, iCoolNumberOfBoxes As Integer = Nothing
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        iCoolNumberOfBoxes = 3 '// set as needed.
        sTemp = "" '// clear for new input.
        For i As Integer = 1 To iCoolNumberOfBoxes
            sTemp &= CType(Me.Controls("TextBox" & i.ToString), TextBox).Text
        Next
        MsgBox(sTemp) '// result.
    End Sub

Ah, thank you very much. Works great :)

:) <<='s: me.looking at the "This thread is solved".
+=1 for me for helping the world "again".:D AndAlso , glad I could help.

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.