Created
December 12, 2017 02:37
-
-
Save drewchapin/cd19f30fd1673747b205028a5fa0e989 to your computer and use it in GitHub Desktop.
Returns all child and grandchild controls of of the specified type.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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