Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class ControlExtender
- {
- /// <summary>
- /// Recursively finds controls of Type T, started from the root.
- /// </summary>
- /// <typeparam name="T">Type of the returned controls</typeparam>
- /// <param name="root">Root control to starts from</param>
- public static IEnumerable<T> GetControlsRecursive<T>(this Control root) where T : Control
- {
- foreach (T control in root.Controls.OfType<T>())
- {
- yield return (T)control;
- if (control.HasControls())
- {
- foreach (T childControl in GetControls<T>(control))
- {
- yield return childControl;
- }
- }
- }
- }
- /// <summary>
- /// Finds controls of Type T.
- /// </summary>
- /// <typeparam name="T">Type of the returned controls</typeparam>
- /// <param name="controlToSearch">Control to search</param>
- public static IEnumerable<T> GetControls<T>(this Control controlToSearch) where T : Control
- {
- foreach (T child in controlToSearch.Controls.OfType<T>())
- {
- yield return (T)child;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement