How to access find all controls and all components into form in C#?

妖精的绣舞 提交于 2019-12-24 08:47:59

问题


I have a window form which contain some Controls an some Components ( like DataTable, XPCollection etc). I would like find all Control Names and Component Names which used into this form.


回答1:


You could do,

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);
   }
}



回答2:


    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);
        }
    }



回答3:


it is explained in this node: Find components on a windows form c# (not controls) It looks there is only way via Reflection available.



来源:https://stackoverflow.com/questions/6736914/how-to-access-find-all-controls-and-all-components-into-form-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!