Possible Duplicate:
how to get all controls of win form?
i have a winform like below in picture.

and i want a list of all the controls of the MainForm.
Like this:
MainForm
Button1
Panel1
TextBox1
Panel2
Button2
TextBox2
Possible Duplicate:
how to get all controls of win form?
i have a winform like below in picture.

and i want a list of all the controls of the MainForm.
Like this:
MainForm
Button1
Panel1
TextBox1
Panel2
Button2
TextBox2
Something like this should work (not perfect code by any means...just meant to get the idea across):
public IEnumerable<Control> GetSelfAndChildrenRecursive(Control parent) { List<Control> controls = new List<Control>(); foreach(Control child in parent.Controls) { controls.AddRange(GetSelfAndChildrenRecursive(child)); } controls.Add(parent); return controls; } var result = GetSelfAndChildrenRecursive(topLevelControl)