zoukankan      html  css  js  c++  java
  • [原] c# winform controls 查找指定类型子控件的扩展方法

    //调用
    this.Controls.Find<Button>(true).ForEach((btn) => { btn.Enabled = false; });
    
    
    //定义
    public static class FormControlExtensions
        {
            /// <summary>
            /// 获得指定类型的孩子控件
            /// </summary>
            /// <typeparam name="TChild">子控件类型</typeparam>
            /// <param name="controlCollection"></param>
            /// <param name="searchAllChildren">如果搜索所有子控件,则为 true;否则为 false。默认为false。</param>
            /// <returns></returns>
            public static List<TChild> Find<TChild>(this Control.ControlCollection controlCollection, bool searchAllChildren = false) 
                where TChild : Control
            {
                var children = new List<TChild>();
                _Find(controlCollection, ref children, searchAllChildren);
                return children;
            }
    
            /// <summary>
            /// 查询指定类型的子控件(支持递归)
            /// </summary>
            /// <typeparam name="TChild">子控件类型</typeparam>
            /// <param name="controlCollection"></param>
            /// <param name="children"></param>
            /// <param name="searchAllChildren">如果搜索所有子控件,则为 true;否则为 false。默认为false。</param>
            private static void _Find<TChild>(Control.ControlCollection controlCollection, ref List<TChild> children, bool searchAllChildren = false) 
                where TChild : Control
            {
                foreach (Control control in controlCollection)
                {
                    if (control is TChild)
                    {
                        children.Add((TChild)control);
                    }
    
                    if (control.Controls.Count > 0 && searchAllChildren)
                    {
                        _Find(control.Controls, ref children, searchAllChildren);
                    }
                }
            }
        }
    

      

  • 相关阅读:
    HDU
    HDU
    (4)数据--相似性与相异性
    (3)数据--操作
    (2)数据--基本概念
    五、按生命周期划分数据(二)
    五、常用数据类型(一)
    四、坏耦合的原因与解耦(三)
    四、强化耦合(二)
    四、初识耦合(一)
  • 原文地址:https://www.cnblogs.com/luqingfei/p/13489764.html
Copyright © 2011-2022 走看看