Click here to Skip to main content
Click here to Skip to main content

How to find all child controls from a starting control or form

By , 27 Feb 2013
Rate this:
Please Sign up or sign in to vote.

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
  ''' <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:

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)

About the Author

Adam Zuckerman
Software Developer (Senior)
United States United States
Long time software engineer who rambles occasionally about coding, best practices, and other random things.
Follow on   Twitter   Google+

Comments and Discussions

 
SuggestionOnly one extension method is needed Pinmemberrm.0416-Jan-14 0:18 
GeneralRe: Only one extension method is needed PinmemberAdam Zuckerman18-Jan-14 15:52 
GeneralMy vote of 4 PinmemberKlaus Luedenscheidt26-Feb-13 19:39 
GeneralRe: My vote of 4 PinmemberAdam Zuckerman27-Feb-13 4:01 
QuestionCode incomplete PinmemberAndrew-Suffolk-UK26-Feb-13 4:31 
AnswerRe: Code incomplete PinmemberAdam Zuckerman26-Feb-13 14:19 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 27 Feb 2013
Article Copyright 2013 by Adam Zuckerman
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid