Jump to content
Xtreme .Net Talk

For each Ctl in xxxx, why doesn't this wok


Recommended Posts

Guest cgchris99
Posted

I am trying to loop thru all the controls that are in a groupbox. Here is what I am trying. This is my test code for debuggin.

 

Dim ctl as Control

Dim x as integer

 

x=form1.instance.GroupBox2.Controls.Count()

 

For Each ctl in Form1.instance.GroupBox2.Controls

myname=ctl.name

next

 

x reports back 76

So I would expect the for loop to loop 76 times. It only loops once.

 

Why is this? I don't know if this helps. But the GroupBox2 is actually on a TabPage also.

 

Thanks

Guest cgchris99
Posted
Yes, I am stepping through it with the debugger. That's how I know it is only happening for one control.
  • *Experts*
Posted

In the declaration of the loop:

For Each ctl in Form1.instance.GroupBox2.Controls

 

I'm pretty sure you need to declare a new instance of Form1,

and then loop through that.

 

Dim ctl as Control 
Dim x as integer 
Dim f As New Form1()

x=f.GroupBox2.Controls.Count() 

For Each ctl in f.GroupBox2.Controls 
myname=ctl.name 
next 

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted (edited)

Sorry for the horrible naming conv.

this works in Net for me

the groupbox has its own controls collection which all objects are added to when they are placed in the groupbox

 

Dim ctr As Control
       For Each ctr In Me.GroupBox1.Controls
           If TypeOf ctr Is Label Then
               ctr.Text = "Cool"
           End If

       Next

 

therefore if you can reference the groupBox object you can refer to its collection property

 

[edit] or loop through collection using the .item[/edit]

       Dim ctr1 As Control
       Dim i As Integer
       Dim l As Integer
       i = GroupBox1.Controls.Count
       If i > 0 Then
           For l = 0 To (i - 1)
               ctr1 = GroupBox1.Controls.Item(l)

           Next
       End If

Edited by duncallen

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