Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi!
I'm new to VB.NET coding, but I have some old knowledge from VB6 and PHP-programming.
I have been trying to figure out how to use 'For' - Loops with multiple text-boxes in my
.NET form. I want it to set the text values by looping thru the number of textboxes specified.I have already tried to find some help on google, but with no luck.
I think this should be basic things, but I can't figure it out.
I am using VB.NET 2005, by the way.Here's my code:
================================ VB.NET CODE ===================================
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Dim e As Integer = i + 1
Dim amTxtBox as Integer = 2
Dim TextObject As String = "TextBox" & e 'This should output: "TextBox1", "TextBox2", "TextBox3"
For i = 0 To amTxtBox '(2 = 3 text boxes)Controls.Item(TextObject).Text() = "Loop Test" & e 'and This should output: TextBox1.Text() = "Loop Test 1", TextBox2.Text() = "Loop Test 2" ,TextBox3.Text() = "Loop Test 3"
Next i
End Sub
===============================================================================Any help is fully appriciated!
.: ThaiCoder :.

I have now figured it out on my own. :-)
So I will share the solution for those who
ever gets the same problem.What I first did was that I tested this code in
a new fresh form and created some text boxes
and with one command button and this code worked out fine:Dim i As Integer
For i = 1 To 3 '"3" stands for the number of the controls to loop. If you want to update the text inside all your text-boxes, then type: Me.Controls.Count -1 instead.
Me.Controls.Item("TextBox" & i).Text() = "Test Loop No." & i
Next i
'[This will output TextBox1.Text()="Test Loop No.1", TextBox2.Text() = "Test Loop No.2", etc.]
After this I was confused why my code didn't work in my original application. And then it struck me! I forgot completely that I had put the Text Boxes inside a Group control, so I retyped my code
like this:For i = 1 To 3 'or use Me.Controls.Count -1
Me.Controls.Item("GroupBox1").Controls.Item("TextBox" & i).Text() = "Test Loop No." & i
Next i
And it all worked perfectly! :-D
.: ThaiCoder :.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |