Looping through TextBoxes

User avatar
Leif
Administrator
Posts: 7206
Joined: 15 Jan 2010, 22:52
Location: Middle of England

Looping through TextBoxes

Post by Leif »

Visual Studio 2008 / VB

I'm new to all this VS stuff - please bear with me :smile:

Is there an easy way to loop through a largish number of TextBoxes to change their text. Currently I have:

Code: Select all

TextBox1.Text = strText
TextBox2.Text = strText
TextBox3.Text = strText
TextBox4.Text = strText
...and instead of having multiple statements, I'd like to create a loop like:

Code: Select all

For intN = 1 to 4
     TextBoxintN.Text = strText
Next
TIA
Leif

User avatar
HansV
Administrator
Posts: 78350
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Looping through TextBoxes

Post by HansV »

Try

Code: Select all

Dim intN As Integer
For intN = 1 To 4
  Controls("TextBox" & intN).Text = strText
Next intN
Best wishes,
Hans

User avatar
Leif
Administrator
Posts: 7206
Joined: 15 Jan 2010, 22:52
Location: Middle of England

Re: Looping through TextBoxes

Post by Leif »

Perfect! Thank you!
Leif

User avatar
Leif
Administrator
Posts: 7206
Joined: 15 Jan 2010, 22:52
Location: Middle of England

Re: Looping through TextBoxes

Post by Leif »

That worked well, but I've now complicated things by moving the textboxes into a Groupbox.

If I address each individually

Code: Select all

TextBox1.Text = strText
it (still) works fine.

If I try and loop through as before (as above)

Code: Select all

Controls("TextBox" & intN).Text = strText
I get an "Object reference not set to an instance of an object." error.

Could somebody help me get my head round this please?
Leif

User avatar
HansV
Administrator
Posts: 78350
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Looping through TextBoxes

Post by HansV »

Let's say the group box is named boxControl. Try the following (it's air code, I don't have VS2008):

Code: Select all

Dim intN As Integer
For intN = 1 To 4
  Me.boxControl.Controls("TextBox" & intN).Text = strText
Next intN
Best wishes,
Hans

User avatar
Leif
Administrator
Posts: 7206
Joined: 15 Jan 2010, 22:52
Location: Middle of England

Re: Looping through TextBoxes

Post by Leif »

Thanks. I've tried this and variations on the theme, but still get the same error. Time for some more textbook reading, methinks....
Leif

User avatar
HansV
Administrator
Posts: 78350
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Looping through TextBoxes

Post by HansV »

Does this work?

Code: Select all

For Each ctl As Control In boxControl.Controls
  If ctl.Name Like "TextBox#" Then
    CType(ctl, TextBox).Text = strText
  End If
Next ctl
or

Code: Select all

For Each ctl As Control In boxControl.Controls
  If TypeOf ctl Is TextBox Then
    CType(ctl, TextBox).Text = strText
  End If
Next ctl
Best wishes,
Hans

User avatar
Leif
Administrator
Posts: 7206
Joined: 15 Jan 2010, 22:52
Location: Middle of England

Re: Looping through TextBoxes

Post by Leif »

Thanks!
After a bit of faffing around with my code, those both work, so I'm happy!

When I find the answer to explicitly address the controls within a loop, I'll post back....
Leif