Click here to Skip to main content
15,884,472 members
Articles / Programming Languages / Visual Basic

How To Find All Child Controls From a Starting Control or Form

Rate me:
Please Sign up or sign in to vote.
4.40/5 (4 votes)
27 Feb 2013CPOL 37.6K   5   6
Here's a post on how to find all child controls from a starting control or form

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.

VB.NET
Module WinformControlExtensions
  ''' <summary>
  ''' Recursively find all child controls for a form
  ''' </summary>
  ''' <param name="StartingContainer"><c><seealso cref="System.Windows.Forms.Form">Form
  ''' </seealso></c> that is the starting container to check for children.</param>
  ''' <returns><c><seealso cref="List(Of System.Windows.Forms.Control)">List(Of Control)
  ''' </seealso></c> that contains a reference to all child controls.</returns>
  ''' <remarks>If you put this module in a separate namespace from your form, Visual Studio 
  ''' 2010 does not recognize the extension to the form.</remarks>
  <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
  ''' <summary>
  ''' Recursively find all child controls for a control
  ''' </summary>
  ''' <param name="StartingContainer"><c><seealso cref="System.Windows.Forms.Control">Control
  ''' </seealso></c> that is the starting container to check for children.</param>
  ''' <returns><c><seealso cref="List(Of System.Windows.Forms.Control)">List(Of Control)
  ''' </seealso></c> that contains a reference to all child controls.</returns>
  ''' <remarks></remarks>
  <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:

VB.NET
Dim oList As List(Of Control) = GroupBox1.FindAllChildren()

License

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


Written By
Software Developer (Senior)
United States United States
Long time software engineer who rambles occasionally about coding, best practices, and other random things.

Comments and Discussions

 
SuggestionOnly one extension method is needed Pin
MrDev16-Jan-14 0:18
MrDev16-Jan-14 0:18 
GeneralRe: Only one extension method is needed Pin
Adam Zuckerman18-Jan-14 15:52
Adam Zuckerman18-Jan-14 15:52 
GeneralMy vote of 4 Pin
Klaus Luedenscheidt26-Feb-13 19:39
Klaus Luedenscheidt26-Feb-13 19:39 
Nice tip. You should file this under Tips/Tricks and not under Articles. Also you should take a look at the source listing. There is a formatting problem.
GeneralRe: My vote of 4 Pin
Adam Zuckerman27-Feb-13 4:01
Adam Zuckerman27-Feb-13 4:01 
QuestionCode incomplete Pin
Andrew-Suffolk-UK26-Feb-13 4:31
Andrew-Suffolk-UK26-Feb-13 4:31 
AnswerRe: Code incomplete Pin
Adam Zuckerman26-Feb-13 14:19
Adam Zuckerman26-Feb-13 14:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.