Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am VB6 developer. Now switch to vb.net. in one situation i feel strange behavior of vb.net
I have group box in group box when i count controls on form by :
Me.controls.count then it returns only one

i have group box with 5 text box + 5 labels (main group box)
then 1 more group box inside main group box named btnFrame(group box for buttons) which have 3 buttons. so total about 15 controls on form.

for same code vb6 returns 15
and vb.net-17 returns 1 only

Basic question is how to iterate using for each loop for all controls?

What I have tried:

i tried
1-me.controls.count but failed.it returns only 1 ( i cannot loop then)
2-then i looped for each ctrl as control in me.controls. also that does not worked.
Posted
Updated 22-Aug-20 21:29pm

1 solution

Me.Controls returns the collection for the Form - i.e. the "outer" GroupBox.
To access the inner group box, you should iterate that collection, and then it's sub-collections:
VB
For Each cOuter As Control In Me.Controls

    If cOuter.HasChildren Then

        For Each cInner As Control In cOuter.Controls

            If cInner.HasChildren Then

                For Each c As Control In cInner.Controls
                    ProcessYourControl(c)
                Next
            End If

            ProcessYourControl(cInner)
        Next
    End If

    ProcessYourControl(cOuter)
Next
The other solution is to write a recursive method that takes a Controls collection as a parameter, and processes all controls within it:
VB
Private Sub FindControls(ByVal controls As ControlCollection)
    For Each c As Control In controls

        If c.HasChildren Then
            FindControls(c.Controls)
        End If

        ProcessYourControl(c)
    Next
End Sub
And then pass that Me.Controls when you call it.
 
Share this answer
 
Comments
hemendrapadia 28-Aug-20 12:17pm    
Yes i tried that recursive but it throws error.
My code is as below:

Sub ManageControlFocusColor(ByVal controls As ControlCollection)

For Each c As Control In controls

If c.HasChildren Then
ManageControlFocusColor(c.Controls) 'Throws error here
End If

If TypeOf c Is TextBox Then
If c Is ActiveControl Then
c.BackColor = Color.LightSteelBlue
Else
c.BackColor = Color.White
End If
End If
'
If TypeOf c Is Button Then
If c Is ActiveControl Then
c.BackColor = Color.LightSteelBlue
Else
c.BackColor = Button.DefaultBackColor
End If
End If
Next
End Sub
----------------------------
Error
----------------------------
System.InvalidCastException: '[A]ControlCollection cannot be cast to [B]ControlCollection. Type A originates from 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'Default' at location 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Type B originates from 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'Default' at location 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'.'
hemendrapadia 28-Aug-20 12:26pm    
But first one is working.
hemendrapadia 29-Aug-20 1:59am    
Sorry, forgot to thank you.
Thank you very much for your response.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900