How to get all controls from a form at runtime in C#
Here is a code snippet which helps you to enumerate all the controls in a form. This snippet can be used for any control, to find the child controls.
public static class Extensions
{
public static void EnumerateChildren(this Control root)
{
foreach (Control control in root.Controls)
{
Console.WriteLine("Control [{0}] - Parent [{1}]",
control.Name, root.Name);
if (control.Controls != null)
{
EnumerateChildren(control);
}
}
}
}
And you can invoke this function like this, to enumerate all the controls of a Form.
this.EnumerateChildren();