Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Whatever thy hand findest to do, do it with all thy heart - Jesus
Posted

Thanks, I figured it out. Instead of creating a Control Collection, I just

declared an array of Controls and indexed through them. =D

 

-=Simcoder=-

Whatever thy hand findest to do, do it with all thy heart - Jesus
Posted

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. :)

Me = 49% Linux, 49% Windows, 2% Hot gas.

 

...Bite me.

 

My Site: www.RedPierSystems.net

-.net, php, AutoCAD VBA, Graphics Design

  • Leaders
Posted

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

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...