This documentation is archived and is not being maintained.

TreeNode.Toggle Method

Toggles the tree node to either the expanded or collapsed state.

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

public void Toggle()

The tree node is toggled to the state opposite its current state, either expanded or collapsed.


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 removes a TreeNode when the user right clicks the mouse over it and toggles it from expanded to collapsed when the user clicks the mouse wheel over it. This example requires that you have a Form with a TreeView control on it. The TreeView should have two or more root tree nodes, each having at least one child node.

private void treeView1_MouseDown(object sender, MouseEventArgs e)
      // Remove the TreeNode under the mouse cursor  
      // if the right mouse button was clicked.  
      case MouseButtons.Right:
         treeView1.GetNodeAt(e.X, e.Y).Remove();

      // Toggle the TreeNode under the mouse cursor  
      // if the middle mouse button (mouse wheel) was clicked.  
      case MouseButtons.Middle:
         treeView1.GetNodeAt(e.X, e.Y).Toggle();

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0