Skip to content

Instantly share code, notes, and snippets.

@polarstars
Created June 27, 2018 07:41
Show Gist options
  • Save polarstars/a205be057ecdef6439c13e5ac373964d to your computer and use it in GitHub Desktop.
Save polarstars/a205be057ecdef6439c13e5ac373964d to your computer and use it in GitHub Desktop.
查找控件包含的子控件(特定类型T) #C#
public static List<T> FindControlByType<T>(this Control mainControl, bool getAllChild = false) where T : Control
{
List<T> lt = new List<T>();
for (int i = 0; i < mainControl.Controls.Count; i++)
{
if (mainControl.Controls[i] is T) lt.Add((T)mainControl.Controls[i]);
if (getAllChild) lt.AddRange(FindControlByType<T>(mainControl.Controls[i], getAllChild));
}
return lt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment