TreeNode.Expand Method

Expands the tree node.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public void Expand ()
public void Expand ()
public function Expand ()
Not applicable.

The Expand method expands the current TreeNode down to the next level of nodes.

NoteNote:

The state of a TreeNode is persisted. For example, if the next level of child nodes was not collapsed previously, when the Expand method is called, the child nodes appear in their previously expanded state.

The following code example toggles the selected node when a button is clicked. If the selected node is collapsed, it is expanded, if it is expanded by calling the Expand method, it is collapsed by calling the Collapse method. This example requires that you have a Form with a TreeView control that has at least one TreeNode with at least one child TreeNode.

private void button1_Click(object sender, System.EventArgs e)
{
   if (treeView1.SelectedNode.IsExpanded)
   {
      treeView1.SelectedNode.Collapse();
      MessageBox.Show(treeView1.SelectedNode.Text + 
        " tree node collapsed.");
   }
   else
   {
      treeView1.SelectedNode.Expand();
      MessageBox.Show(treeView1.SelectedNode.Text + 
        " tree node expanded.");
   }
}

private void button1_Click(Object sender, System.EventArgs e)
{
    if (treeView1.get_SelectedNode().get_IsExpanded()) {
        treeView1.get_SelectedNode().Collapse();
        MessageBox.Show(treeView1.get_SelectedNode().get_Text()
            + " tree node collapsed.");
    }
    else {
        treeView1.get_SelectedNode().Expand();
        MessageBox.Show(treeView1.get_SelectedNode().get_Text()
            + " tree node expanded.");
    }
} //button1_Click

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Show: