Simcoder Posted June 7, 2005 Posted June 7, 2005 I've been fighting with this for quite some time now, Can someone explain to me if I'm doing this right of if this is even possible. I have a form with several textboxes. Is there a way to index through them in a loop. Here's an example. Dim Controls As ControlCollection Controls.Add(txtControl) Controls.Add(txtSerial) Controls.Add(txtModel) Dim I As Integer For I = 0 To 2 Controls.Item(I).Text = "I Am TextBox " & I Next Now txtControl, txtSerial, txtModel are already set up on my form. I'm looking for a way to loop through them and change the text. Thanks In Advance. Simcoder Quote Whatever thy hand findest to do, do it with all thy heart - Jesus
Administrators PlausiblyDamp Posted June 7, 2005 Administrators Posted June 7, 2005 You could just use a for ... each loop. For Each c As Control In Controls c.Text = "Blah!" Next Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Simcoder Posted June 8, 2005 Author Posted June 8, 2005 Thanks, I figured it out. Instead of creating a Control Collection, I just declared an array of Controls and indexed through them. =D -=Simcoder=- Quote Whatever thy hand findest to do, do it with all thy heart - Jesus
ALEX_0077 Posted June 9, 2005 Posted June 9, 2005 Just some more info: If its not too many controls, i guess you could also do this: Public Sub SomeMethod() Dim obj As Object For Each Obj In frmTheForm.Controls If GetType(Obj).ToString.ToLower = "System.Windows.Forms.TextBox" Then Dim txt = DirectCast(Obj, System.Windows.Forms.TextBox) 'Do some stuff with your txt object End If Next Object End Sub Of course, you have to see if its a collection of type OBJECT, or some other type. Set the 'obj' variable's type accordingly. :) Quote Me = 49% Linux, 49% Windows, 2% Hot gas. ...Bite me. My Site: www.RedPierSystems.net -.net, php, AutoCAD VBA, Graphics Design
Leaders snarfblam Posted June 9, 2005 Leaders Posted June 9, 2005 You can save yourself some typing with the TypeOf...Is operator. Public Sub SomeMethod() For Each Obj As Control In frmTheForm.Controls If TypeOf Obj Is TextBox Then Dim txt As TextBox = DirectCast(Obj, TextBox) 'Do some stuff with your txt object End If Next End Sub Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.