-3

Possible Duplicate:
how to get all controls of win form?

i have a winform like below in picture.
enter image description here
and i want a list of all the controls of the MainForm.
Like this:
MainForm
Button1
Panel1
TextBox1
Panel2
Button2
TextBox2

Community
  • 1
  • 1
namco
  • 6,208
  • 20
  • 59
  • 83

1 Answers1

7

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)
eMi
  • 5,540
  • 10
  • 60
  • 109