Control array

Ruud

Member
Joined
Jan 2, 2007
Messages
10
Programming Experience
1-3
if i have textbox1, textbox2,...textBox5

so, how do this in vb.net:

for i as Integer =1 to 5

TextBox(i).Text=i

next



Thanks very much
 
One possible approach would be to: Place all your textboxes in a panel and then loop through the panels controls collection with a for each loop.
 
I have seen this very simple solution from of the admins a few days ago but I couldn't find where, in order to insert a link :

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Tb [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TextBox[/SIZE]
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] I [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 1 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 5[/SIZE]
[SIZE=2]Tb = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Controls([/SIZE][SIZE=2][COLOR=#a31515]"Textbox"[/COLOR][/SIZE][SIZE=2] & I.ToString)[/SIZE]
[SIZE=2]Tb.Text = I.ToString[/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
Of course you will have an error if you try to use a name that is not in the control collection of your form.
 
Back
Top