Or for the C# impaired here's the same code in VB syntax
' Air code warning!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
enumerateControls(Me.Controls)
End Sub
Private Sub enumerateControls(ByVal container As ControlCollection)
Dim c As Control
For Each c In container
' Do something with control here
' Now check this control to see if it has any controls of
its own
If c.Controls.Count 0 Then
enumerateControls(c.Controls)
End If
Next
End Sub
Thanks,
Seth Rowe
Chris Dunaway wrote:
Hamed wrote:
Hello
I have a form that containes a lot of controls including panels those
contain other controls. Is there a way to simply get all contained controls
either directly or indirectly through other containers such as panel or
GroupBox controls?
Use a recursive function and start with the form:
private void button1_Click(object sender, EventArgs e)
{
enumerateControls(this.Controls);
}
private void enumerateControls(ControlCollection container)
{
foreach(Control c in container)
{
//Do something with control here
//Now check this control to see if it has any controls of its own
if (c.Controls.Count 0)
enumerateControls(c.Controls);
}
}
Hope this helps and watch for typos