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

I have a window form that contains some controls (some elements) (e.g. DataTable, XPCollection, etc.). I would like to find all the Management Names and Component Names that were used in this form.

+1
source share
3 answers

You can 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); } } 
+1
source
  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); } } 
0
source

this is explained in this node: Find components in Windows C # form (not manage) It seems that there is only a path through Reflection.

0
source

All Articles