Easier Way To Read From Multiple Textboxes
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
Wolxhound90
Junior Poster in Training
55 posts since Mar 2011
Reputation Points: 10
Solved Threads: 1
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
codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
Ah, thank you very much. Works great :)
Wolxhound90
Junior Poster in Training
55 posts since Mar 2011
Reputation Points: 10
Solved Threads: 1
:) <<='s: me.looking at the "This thread is solved".
+=1 for me for helping the world "again".:D
AndAlso , glad I could help.
codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384