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

  • Thread starter Thread starter cgchris99
  • Start date Start date
C

cgchris99

Guest
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
 
It ought to loop however many times there are controls in the groupbox. I see no code for counting the number of loops, so how do you know. You step through it?
 
Yes, I am stepping through it with the debugger. That's how I know it is only happening for one control.
 
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.

Visual Basic:
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
 
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

Visual Basic:
 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]
Visual Basic:
        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
 
Last edited:
Back
Top