I created a couple of extension methods that allow my program to find all of the controls from either a form or a containing control. This code is targeted for .NET 4 Client, but should also work in .NET 3.5.
Module WinformControlExtensions
<System.Runtime.CompilerServices.Extension()>
Public Function FindAllChildren(ByRef StartingContainer As System.Windows.Forms.Form)
As List(Of System.Windows.Forms.Control)
Dim Children As New List(Of System.Windows.Forms.Control)
Dim oControl As System.Windows.Forms.Control
For Each oControl In StartingContainer.Controls
Children.Add(oControl)
If oControl.HasChildren Then
Children.AddRange(oControl.FindAllChildren())
End If
Next
Return Children
End Function
<System.Runtime.CompilerServices.Extension()>
Public Function FindAllChildren(ByRef StartingContainer As System.Windows.Forms.Control)
As List(Of System.Windows.Forms.Control)
Dim Children As New List(Of System.Windows.Forms.Control)
If StartingContainer.HasChildren = False Then
Return Nothing
Else
Dim oControl As System.Windows.Forms.Control
For Each oControl In StartingContainer.Controls
Children.Add(oControl)
If oControl.HasChildren Then
Children.AddRange(oControl.FindAllChildren())
End If
Next
End If
Return Children
End Function
End Module
An example usage from a groupbox
:
Dim oList As List(Of Control) = GroupBox1.FindAllChildren()