TreeView.CollapseAll Method

Collapses all the tree nodes.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public void CollapseAll()

The CollapseAll method collapses all the TreeNode objects, which includes all the child tree nodes, that are in the TreeView control.

NoteNote

The state of a TreeNode persists. For example, suppose that you call the Expand method for a root tree node. If the child tree nodes were not previously collapsed, they will appear in their previously-expanded state. Calling the CollapseAll method ensures that all the tree nodes appear in the collapsed state.

The following code example demonstrates how to change the collapse state of a TreeView so that all the checked nodes are visible. First, all the nodes are collapsed, and a handler is added for the BeforeExpand event. Next, all the nodes are expanded. The BeforeExpand event handler determines whether a given node has child nodes that are checked. If a node does not have checked children, the expansion is canceled for that node. In order to allow standard node expansion when the plus sign next to a node is clicked, the BeforeExpand event handler is then removed.

This behavior can also be implemented by handling the BeforeCollapse event, as illustrated in the example for that topic.

For the complete example, see the CheckBoxes reference topic.

private void showCheckedNodesButton_Click(object sender, EventArgs e)
{
    // Disable redrawing of treeView1 to prevent flickering  
    // while changes are made.
    treeView1.BeginUpdate();

    // Collapse all nodes of treeView1.
    treeView1.CollapseAll();

    // Add the checkForCheckedChildren event handler to the BeforeExpand event.
    treeView1.BeforeExpand += checkForCheckedChildren;

    // Expand all nodes of treeView1. Nodes without checked children are  
    // prevented from expanding by the checkForCheckedChildren event handler.
    treeView1.ExpandAll();

    // Remove the checkForCheckedChildren event handler from the BeforeExpand  
    // event so manual node expansion will work correctly.
    treeView1.BeforeExpand -= checkForCheckedChildren;

    // Enable redrawing of treeView1.
    treeView1.EndUpdate();
}

// Prevent expansion of a node that does not have any checked child nodes. 
private void CheckForCheckedChildrenHandler(object sender, 
    TreeViewCancelEventArgs e)
{
    if (!HasCheckedChildNodes(e.Node)) e.Cancel = true;
}

// Returns a value indicating whether the specified  
// TreeNode has checked child nodes. 
private bool HasCheckedChildNodes(TreeNode node)
{
    if (node.Nodes.Count == 0) return false;
    foreach (TreeNode childNode in node.Nodes)
    {
        if (childNode.Checked) return true;
        // Recursively check the children of the current child node. 
        if (HasCheckedChildNodes(childNode)) return true;
    }
    return false;
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft