以下函数的作用为 遍历容器,改变容器内所有T类型控件的文字颜色

/// <summary>
/// 遍历fatherControl内所有T类型控件,改变字体颜色
/// </summary>
/// <typeparam name="T">要改变字体颜色的类型</typeparam>
/// <param name="fatherControl">要遍历的控件</param>
/// <param name="Col">改变的颜色</param>
private void GetControls1<T>(Control fatherControl, Color Col) where T : Control
{
    Control.ControlCollection sonControls = fatherControl.Controls;
    //遍历所有控件  
    foreach (Control control in sonControls)
    {
        if (control is T)
        {
            (control as T).ForeColor = Col;
        }
        if (control.Controls != null)
        {
            GetControls1<T>(control, Col);
        }
    }
}
where T : Control 为泛型约束,传入的类型T仅为Control 或它的子类
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐