[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Display a form's control hierarchy in C#

[Display a form's control hierarchy in C#]

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); trv.ExpandAll(); // Display the hierarchy. dlg.ShowDialog(); }

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 = parent_node.Nodes.Add(ControlName(child)); 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 the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.