Question How To Enable multiple text boxes?

paris_tj

Member
Joined
Mar 2, 2010
Messages
7
Programming Experience
Beginner
I have 7 textbox's.

Names are
textbox1
textbox2
textbox3
textbox4
and so on...

I need to do


For i = 1 to MyVar
textbox(i).enabled
next

Not sure how to do this, any advice is appreciated.
 
How about:

VB.NET:
For i As Integer = 1 To myVar
   CType(Me.Controls("textbox" & i.ToString), Textbox).Enabled = False
Next
 
Not working...

VB.NET:
theWord = "Winders"
For i As Integer = 1 To theWord.Length
     CType(Me.Controls("textbox" & i), TextBox).Enabled = True
Next

Error:
Conversion from string "textbox1" to type 'Integer' is not valid.
 
I think you missing something. Using Me.Controls(?) takes either an index or the name. "i" is an Integer and you need to call it's .ToString() method to complete the name of the control. Try it exactly as I posted.
 
The String indexer of Controls collection was added in .Net 2.0, if paris_tj only has the Integer indexer it must mean .Net 1.0 or .Net 1.1, and not .Net 3.5 as profile states. If this is true it's upgrade time! ;)
 
Use a For Each block to access each of the textboxes:


VB.NET:
		For Each ctl As Control In Controls
			If TypeOf ctl Is TextBox Then
				ctl.Enabled = True
			End If
		Next ctl
 
Back
Top