Help in getting all the controls in a form

I want to know how to get all the controls in a form beside using "for each control...next control" ? can anyone help me?
[131 byte] By [jerjer] at [2007-12-24]
# 1
Each container control (including forms) have a Controls property which returns a collection of the child controls of the container. You would need to loop through this recursively to get all of the controls.
WilliamBartholomew at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

You *could* store them in a collection:

Dim theControls as ControlCollection = Me.Controls

but it only stores them in the array. To go through each control, you would need to iterate through each control

ahmedilyas at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3

William Bartholomew wrote:
Each container control (including forms) have a Controls property which returns a collection of the child controls of the container. You would need to loop through this recursively to get all of the controls.

tnx, but can you give me a sample code. i have a hard time doing it. i need to get the controls inside the controls too.

jerjer at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4

but you stated you didnt want to use a for each loop to go through the controls? There is no other way of doing so in this case....apart from using the for each loop

for each currentControl as Control in Me.Controls

if currentControl.GetType Is GetType(SomeControl) then

Dim theControl as SomeControl = CType(currentControl, SomeControl)

'do whatever with the current control

end if

next

replace "SomeControl" with the control you are after, such as TextBox or Button or Label....

ahmedilyas at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5

Dim allControls As New Collection(Of Control)
EnumerateControls(Me, allControls)

Private Sub EnumerateControls( ByVal container As Control, ByVal controlCollection As Collection( Of Controls ) )
For Each currentControl In container.Controls
controlCollection.Add( currentControl )
EnumerateControls( currentControl, controlCollection )
Next
End Sub

WilliamBartholomew at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6

thanks guys. here's what i got from your ideas

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

For Each ctl As Control In Me.Controls

MsgBox(ctl.Name, MsgBoxStyle.Information, "Root")

ec(ctl, 1)

Next

End Sub

Private Sub ec(ByVal mctl As Control, ByVal i As Integer)

For Each xctl As Control In mctl.Controls

Select Case TypeName(xctl)

Case "Tabcontrol"

ec(xctl, i)

Case "TabPage"

ec(xctl, i)

i = i + 1

Case "Panel", "TableLayoutPanel", "GroupBox"

ec(xctl, i)

i = i + 1

Case "Label", "Button", "RadioButton", "CheckBox"

i = i + 1

End Select

MsgBox(xctl.Name, MsgBoxStyle.Information, "level " & i)

Next

End Sub

jerjer at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...