Newbie question - VB.net, referencing controls

J

jkbat

Hi,

Hoping this isn't too trivial a question:

I have a form with TextBoxes named TextBox1, TextBox2, etc. I'd like to
iterate through a whole series of these and set their values based
(initially) on a counter. I'm thinking I need a way to convert a string
to a control's reference?

What I want to do in (pseudo-ish) code is:

Dim str as String
Dim i as Integer

Do
Me.TextBox(i).Text = i
Loop Until i = 32

I can generate the appropriate string variable with the value I need
i.e.
str = "Me.TextBox" & i & ".Text"

but it's not being picked up as a control reference. I've had a look at
DirectCast, but can't seem to figure it out...

Any ideas?

Cheers,

Jimmy
 
C

Cor Ligthert [MVP]

JKbat,

Herfried will show for sure a link, but if the textboxes are in a panel let
say panel1, than you can do

dim i as integer = 1
for each txtb as control in panel1
txtb.text = i.tostring
i += 1
next

On this are endless variations, but because that Herfried will show that
link anyhow I keep it with this,

I hope this helps,

Cor
 
G

Guest

well i dunno if you can reference a control by a variable name....id try it
this way

For Each txt As TextBox In Me.Controls

Select Case txt.Name

.....

End Select

Next


hope that helps
 
H

Herfried K. Wagner [MVP]

jkbat said:
I have a form with TextBoxes named TextBox1, TextBox2, etc. I'd like to
iterate through a whole series of these and set their values based
(initially) on a counter. I'm thinking I need a way to convert a string
to a control's reference?

What I want to do in (pseudo-ish) code is:

Dim str as String
Dim i as Integer

Do
Me.TextBox(i).Text = i
Loop Until i = 32

Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>
 
J

jkbat

Thanks to all for the input! I've used a bits from the suggestions
provided; here's what I've got to work:

Dim str As String
Dim ctr As Control
Dim i As Integer = 1

For Each ctr In Me.Controls
If TypeOf ctr Is TextBox Then
str = "TextBox" & i
DirectCast(FindControl(str, Me), TextBox).Text = str
i += 1
End If
Next

All the best,

Jimmy
 
C

Cor Ligthert [MVP]

Jimmy,

The DirectCast to Textbox is not needed in this situation.
Text is a in textbox derived property from control and can therefore direct
be used.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.text.aspx

I thought it would be helpful to know.

Cor



jkbat said:
Thanks to all for the input! I've used a bits from the suggestions
provided; here's what I've got to work:

Dim str As String
Dim ctr As Control
Dim i As Integer = 1

For Each ctr In Me.Controls
If TypeOf ctr Is TextBox Then
str = "TextBox" & i
DirectCast(FindControl(str, Me), TextBox).Text = str
i += 1
End If
Next

All the best,

Jimmy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top