none
这句代码的详细解释List<Control> ControlList = new List<Control>(); 是什么 RRS feed

答案

  • 构造一个控件的列表

    常用在一些基类窗体(Base form,form base)方法中,比如枚举当前窗体中的所有控件

    List<Control> ControlList = Controls.OfType<TextBox>().Cast<Control>().ToList();

    或是枚举文本控件

    List<Control> ControlList = new List<Control>();
    private void GetAllControls(Control container)
    {
       
    foreach (Control c in container.Controls)
       
    {
           
    GetAllControls(c);
           
    if (c is TextBox) ControlList.Add(c);
       
    }
    }

    private IEnumerable<Control> GetAllTextBoxControls(Control container)
    {
       
    List<Control> controlList = new List<Control>();
       
    foreach (Control c in container.Controls)
       
    {
            controlList
    .AddRange(GetAllTextBoxControls(c));
           
    if (c is TextBox)
                controlList
    .Add(c);
       
    }
       
    return controlList;
    }

    试着跑一下这些代码片段

    List<Control> allTextboxes = GetAllControls(this);

       
    private List<Control> GetAllControls(Control container, List<Control> list)
       
    {
           
    foreach (Control c in container.Controls)
           
    {
               
    if (c is TextBox) list.Add(c);
               
    if (c.Controls.Count > 0)
                    list
    = GetAllControls(c, list);
           
    }

           
    return list;
       
    }
       
    private List<Control> GetAllControls(Control container)
       
    {
           
    return GetAllControls(container, new List<Control>());
       
    }


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms


    2016年8月17日 2:49

全部回复

  • 构造一个控件的列表

    常用在一些基类窗体(Base form,form base)方法中,比如枚举当前窗体中的所有控件

    List<Control> ControlList = Controls.OfType<TextBox>().Cast<Control>().ToList();

    或是枚举文本控件

    List<Control> ControlList = new List<Control>();
    private void GetAllControls(Control container)
    {
       
    foreach (Control c in container.Controls)
       
    {
           
    GetAllControls(c);
           
    if (c is TextBox) ControlList.Add(c);
       
    }
    }

    private IEnumerable<Control> GetAllTextBoxControls(Control container)
    {
       
    List<Control> controlList = new List<Control>();
       
    foreach (Control c in container.Controls)
       
    {
            controlList
    .AddRange(GetAllTextBoxControls(c));
           
    if (c is TextBox)
                controlList
    .Add(c);
       
    }
       
    return controlList;
    }

    试着跑一下这些代码片段

    List<Control> allTextboxes = GetAllControls(this);

       
    private List<Control> GetAllControls(Control container, List<Control> list)
       
    {
           
    foreach (Control c in container.Controls)
           
    {
               
    if (c is TextBox) list.Add(c);
               
    if (c.Controls.Count > 0)
                    list
    = GetAllControls(c, list);
           
    }

           
    return list;
       
    }
       
    private List<Control> GetAllControls(Control container)
       
    {
           
    return GetAllControls(container, new List<Control>());
       
    }


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms


    2016年8月17日 2:49
  • Hi,

    意思就是创建一些控件,并且把塔放到list列表里面。

    方便我们使用和控制这个控件。

    Best Regards,

    Hart


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    2016年9月1日 2:13
    版主