Skip to content

Instantly share code, notes, and snippets.

@drewchapin
Created December 12, 2017 02:37
Show Gist options
  • Save drewchapin/cd19f30fd1673747b205028a5fa0e989 to your computer and use it in GitHub Desktop.
Save drewchapin/cd19f30fd1673747b205028a5fa0e989 to your computer and use it in GitHub Desktop.
Returns all child and grandchild controls of of the specified type.
/// <summary>
/// Returns all child and grandchild controls of of the specified type.
/// </summary>
/// <typeparam name="T">Type of control to look for</typeparam>
/// <param name="parent">Parent to get list of controls from</param>
/// <returns></returns>
public static IEnumerable<T> ControlsOfType<T>( this Control parent ) where T : Control
{
foreach( Control child in parent.Controls )
{
if( child is T )
yield return child as T;
foreach( Control grandchild in child.ControlsOfType<T>() )
yield return grandchild as T;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment