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.
Recursion is the very useful concept in the programming that cause a function to call itself. I am using this concept in the above
For more information about Recursion : http://msdn.microsoft.com/en-us/library/vstudio/z3dk2cc3(v=vs.100).aspx