如何在C#中查找所有控件和所有组件到表单?

时间:2011-07-18 17:38:11

标签: c# winforms forms reflection

我有一个窗口表单,其中包含一些控件和一些组件(如DataTable,XPCollection等)。我想找到用于此表单的所有控件名称和组件名称。

3 个答案:

答案 0 :(得分:1)

你可以,

List<string> ctrlNames = new List<string>();
FIndAllCtrls(ctrlNames , this.Controls);

private void FIndAllCtrls(ctrlNames, ControlCollection ctrlColl)
{
   foreach(Control ctrl in ctrlColl)
   {
      ctrlNames.Add(ctrl.Name);
      if(ctrl.Controls.Count > 0)
         FIndAllCtrls(ctrlNames, ctrl.Controls);
   }
}

答案 1 :(得分:0)

    IEnumerable<Control> EnumControls(Control top)
    {
        Queue<Control> todo = new Queue<Control>();
        todo.Enqueue(top);

        while (todo.Count > 0)
        {
            Control c = todo.Dequeue();

            yield return c;
            foreach (Control ch in c.Controls)
                todo.Enqueue(ch);
        }
    }

答案 2 :(得分:0)

在此节点中解释: Find components on a windows form c# (not controls) 它看起来只有通过反射可用的方式。

相关问题