-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

1
  • Just loop through the controls that exist on the MainForm, Panel1, and Panel2. Commented Dec 18, 2012 at 12:41

1 Answer 1

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) 
Sign up to request clarification or add additional context in comments.

3 Comments

parent control should gobefore children?
mhm shouldn't be really important here :)
just a suggestion :) already gave +1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.