using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Common
{
/// <summary>
/// 获取指定类型控件
/// </summary>
public static class LoopControls
{
public static IEnumerable<T> AllControl<T>(this Control startingPoint) where T : Control
{
bool hit = startingPoint is T;
if (hit)
{
yield return startingPoint as T;
}
foreach (var child in startingPoint.Controls.Cast<Control>())
{
foreach (var item in AllControls<T>(child))
{
yield return item;
}
}
}
}
}
var checkboxes = LoopControls.AllControls<CheckBox>(this);
foreach (CheckBox c in checkboxes)
{
}