HOW TO FIND CONTROLS IN WINFORMS C#

Goti Bandu

Total Post:119

Points:835
Posted by  Goti Bandu
C# 
C#
 2000  View(s)
Ratings:
Rate this:

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...

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: How to find controls in winforms c#

    Hi Goti,


    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);

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!