Make a list of checked TreeView nodes in C#

[example]

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 see the details.


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 algorithms, controls, recursion, user interface and tagged , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *