Computing.Net > Forums > Programming > Help - loop thru textboxes - vb.net

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help - loop thru textboxes - vb.net

Reply to Message Icon

Name: ThaiCoder
Date: November 16, 2008 at 08:24:20 Pacific
OS: XP64SP2/OSX10.5
CPU/Ram: Intel Core Duo 2.1 GHz /
Product: APPLE / Macbook 2.1,4
Comment:

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 :.



Sponsored Link
Ads by Google

Response Number 1
Name: ThaiCoder
Date: November 17, 2008 at 02:47:31 Pacific
Reply:

Is there any one who has done this before?

.: ThaiCoder :.


0

Response Number 2
Name: ThaiCoder
Date: November 17, 2008 at 05:15:55 Pacific
Reply:

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 :.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Help - loop thru textboxes - vb.net

textbox in VB.net www.computing.net/answers/programming/textbox-in-vbnet/16962.html

Help with Date in VB.net www.computing.net/answers/programming/help-with-date-in-vbnet/14259.html

Knowledge Based System in VB.net www.computing.net/answers/programming/knowledge-based-system-in-vbnet/13585.html