[C# Helper]
Index Books FAQ Contact About Rod
[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]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

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

Title: Make a list of checked TreeView nodes in C#

[Make a list of checked TreeView nodes in C#]

If you set a TreeView control's CheckBoxes property to true, then the control displays boxes that the user can check to select nodes. In that case you will probably need to find the checked TreeView nodes at some point.

Strangely the TreeView control doesn't provide a simple method for finding the checked TreeView nodes. In fact, it doesn't even have a simple way to enumerate the control's nodes so you can see which ones are checked. Both of those omissions seem odd. After all, the ListBox control provides five properties that help you figure out which items are selected: SelectedIndex, SelectedIndices, SelectedItem, SelectedItems, and SelectedValue.

One way to find the checked TreeView nodes is to recursively crawl over the tree's nodes and find them. The TreeView control has a Nodes property. That property has type TreeNodeCollection, and it contains the control's top-level nodes.

Each node in the tree has a similar Nodes property that contains its child nodes.

To enumerate every node in the tree, you can write a method that enumerates the nodes within a TreeNodeCollection and their descendants in the tree. This example uses the following method to build a list of the checked TreeView nodes.

// Return a list of the TreeNodes that are checked. private void FindCheckedNodes( List<TreeNode> checked_nodes, TreeNodeCollection nodes) { foreach (TreeNode node in nodes) { // Add this node. if (node.Checked) checked_nodes.Add(node); // Check the node's descendants. FindCheckedNodes(checked_nodes, node.Nodes); } }

The method takes as a parameter a List<TreeNode> where it will place the checked TreeView nodes. It also takes as a parameter a TreeNodeCollection.

The method loops through the nodes in the collection. It checks each node and then recursively calls itself to check the node's children stored in its Nodes property.

The following method wraps the call to FindCheckedNodes for the TreeView control.

// Return a list of the checked TreeView nodes. private List<TreeNode> CheckedNodes(TreeView trv) { List<TreeNode> checked_nodes = new List(); FindCheckedNodes(checked_nodes, trvMeals.Nodes); return checked_nodes; }

This method simply creates a List<TreeNode> and then calls the FindCheckedNodes method for the TreeView control's Nodes collection.

When you click the program's Show Checked button, it uses the following code to get the list of checked TreeView nodes.

// Get the checked nodes. List<TreeNode> checked_nodes = CheckedNodes(trvMeals);

The code that displays the list is straightforward so it isn't shown here.

Download the example to experiment with it and to see additional details.

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