How to get all controls from a form at runtime in C#

Posted by Anuraj on Tuesday, March 26, 2013 Reading time :1 minute

.Net .Net 3.0 / 3.5 Miscellaneous Windows Forms

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)

And you can invoke this function like this, to enumerate all the controls of a Form.


What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.

Similar Posts

Did you like this article? Share it with your friends

Facebook Twitter Google+ LinkedIn Reddit StumbleUpon

BMC logoBuy me a coffee

Copyright © 2019 - Anuraj P. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Hosted with ❤ by GitHub