TreeView.ExpandAll Method

Definition

Expands all the tree nodes.

C#
public void ExpandAll();

Examples

The following code example expands all the tree nodes in a TreeView control when a CheckBox is checked, and collapses the FirstNode when the CheckBox is cleared. This example requires that you have a Form with a CheckBox, and a TreeView control with a TreeNodeCollection that has several TreeNode objects (preferably with three or more levels).

C#
private void myCheckBox_CheckedChanged(object sender, System.EventArgs e)
{
   // If the check box is checked, expand all the tree nodes.
   if (myCheckBox.Checked)
   {
      myTreeView.ExpandAll();
   }
   else
   {
      // If the check box is not checked, collapse the first tree node.
      myTreeView.Nodes[0].FirstNode.Collapse();
      MessageBox.Show("The first node of CustomerList root node is collapsed");
   }
}

Remarks

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

Note

The state of a TreeNode persists. For example, suppose that you call the ExpandAll method, and then the individual root tree nodes are collapsed. The child tree nodes have not been collapsed, and will appear in their previously-expanded state when the root tree nodes are expanded again. Calling the CollapseAll method ensures that all the tree nodes appear in the collapsed state.

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also