Display a form’s control hierarchy in C#

[control hierarchy]

This example displays the control hierarchy for a form. It’s actually relatively simple.

Some controls can contain other controls. For example, a form is a control and it contains all of a Windows Forms application’s controls. Similarly Panel, TableLayoutPanel, SplitContainer, and other controls can hold child controls to form a control hierarchy.

This program includes an assortment of controls containing each other. When you click the button in the upper left corner, the following code executes.

// Show the form's control hierarchy in a TreeView control.
private void btnShowHierarchy_Click(object sender, EventArgs e)
    // Make the root node.
    HierarchyForm dlg = new HierarchyForm();
    TreeView trv = dlg.trvHierarchy;
    TreeNode root = trv.Nodes.Add(ControlName(this));

    // Build the rest of the hierarchy.
    BuildHierarchy(root, this);

    // Display the hierarchy.

This code creates a new HierarchyForm. That is a form that I added to the project to display a form’s control hierarchy. (I decided that it would be too confusing to try to display a form’s control hierarchy on the form itself.)

The HierarchyForm only contains a TreeView control. Note that I set that control’s Modifiers property to Public at design time. That allows the main form’s code to access the TreeView control.

After it creates the HierarchyForm control, the code gets a reference to that TreeView control. It adds a root node containing form’s name. To do that, it uses the ControlName method described shortly to get the form’s name.

The code then calls the BuildHierarchy method (also described shortly) to build the rest of the tree that displays the control hierarchy. This code finishes by expanding all of the TreeView control’s nodes and displaying the HierarchyForm.

The following code shows the BuildHierarchy method.

// Show the ctl control's hierarchy.
private void BuildHierarchy(TreeNode parent_node, Control parent)
    foreach (Control child in parent.Controls)
        TreeNode child_node =
        BuildHierarchy(child_node, child);

This method takes as parameters a node in the TreeView and a parent control. (In the first call to this method, the parent control is the form.)

The code loops through the child controls that are contained by the parent. For each child (if there are any children), the method adds a new node to the parent TreeView node. it then recursively calls the BuildHierarchy method to add the child’s children to the tree.

The last piece of the program is the following ControlName method.

// Return the control's name and type.
private string ControlName(Control ctl)
    return ctl.Name + " [" + ctl.GetType().Name + "]";

This method simply returns a control’s name followed by its type in square brackets.

That’s all there is to it! The only part that’s even a bit confusing is the way the program uses recursion to traverse the control hierarchy and add nodes to the tree.

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in controls, forms and tagged , , , , , , , , , , , , . Bookmark the permalink.