vb.net help

rockwell

Active member
Joined
Dec 6, 2005
Messages
36
Programming Experience
Beginner
Hi, i have 10 textboxes names txtbox1 to txtbox10. want to assign a value 1 in the first textbox, 2 in the second and soon. I wrote a for loop and tried to concat i with txtbox but gave an error

for i=0 to 11
txtbox & i.text = i
next

the above code gave an error, if some one can help me out in this it would be great.

Thanks for your time ....
 
This should do the trick....

VB.NET:
Dim A As Integer = 1
For each Ctrl as control in me.controls
if TypeOf Ctrl is textbox then
ctrl.text = Convert.ToString(A)
A+=1
End If
Next
 
you have 10 text boxes, but you are going through your loop 12 times. You are probably getting an error because you are trying to assign the values of 0 and 11 to txtbox0 and txtbox11, these text boxes do not exist.just change your existing code to this:
for i=1 to 10
txtbox & i.text = i
next i
 
Last edited by a moderator:
cBarry263 said:
you have 10 text boxes, but you are going through your loop 12 times. You are probably getting an error because you are trying to assign the values of 0 and 11 to txtbox0 and txtbox11, these text boxes do not exist.just change your existing code to this:
for i=1 to 10
txtbox & i.text = i
next i

actually his code shouldnt even compile because the IDE should underline this:
txtbox & i.text
 
Just declare a TextBox array as a class level variable. Assign your TextBoxes to the elements in the form constructor or Load event handler. Now you can simply iterate over the elements of the array.
 
Back
Top