C# 获取控件下指定类型的控件

C# 获取控件下指定类型的控件

获取控件组合后,统一进行处理。

public static class ControlExtension
{
    public static IEnumerable<T> CollectControls<T>(this Control control)
    {
        var controls = control.Controls.OfType<T>();
        foreach (var item in controls)
        {
            yield return item;
        }

        foreach (Control subControl in control.Controls)
        {
            var resultControls = CollectControls<T>(subControl);
            foreach (var item in resultControls)
            {
                yield return item;
            }
        }
    }
}
打赏