1

i need to find controls in my forms. in asp.net i used Recursive method to do it.now how to do in winforms

public static Control FindControlRecursive(Control root, string id)
{
    if (root.ID == id)
        return root;

    return root.Controls.Cast<Control>()
       .Select(c => FindControlRecursive(c, id))
       .FirstOrDefault(c => c != null);
}

any idea..thanks...

Happy
  • 1,105
  • 5
  • 17
  • 25
  • 1
    http://stackoverflow.com/questions/3419159/how-to-get-all-child-controls-of-a-windows-forms-form-of-a-specific-type-button – Nitin Varpe Jan 22 '14 at 07:31

2 Answers2

2

Each control has a Controls property which in fact is a ControlCollection. This collection itself has a method Find() which takes 2 parameters. The first parameter is the name of the control which you want to find, the second parameter indicates wether to include all children into the search.

As a sample:

Control[] allButton1 = this.Controls.Find("button1", true);
// for your example
Control[] foundControls = this.Controls.Find(root.Name,true);
Heslacher
  • 2,167
  • 22
  • 37
0

You can use root.Name in place of root.ID in win forms.

Ravi Verma
  • 182
  • 1
  • 3
  • 12