How to get the list of all controls of the form

The following code snippet can be use to get the list of all controls of the Windows form. In the example we are using the concept of the recursion.
 

private List<Control> GetAllControls(Control container, List<Control> list)
 {
   foreach (Control c in container.Controls)
    {
       list.Add(c);
       if (c.Controls.Count > 0)
         list = GetAllControls(c, list);
    }
    return list;
}

 
How to use the above function:
 

List<Control> AllControlsCollection = GetAllControls(this, new List<Control>());

 
You can get the list of the all controls in the AllControlCollection object.

One thought on “How to get the list of all controls of the form”

Comments are closed.