This documentation is archived and is not being maintained.

TreeNode.Toggle Method

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

[Visual Basic]
Public Sub Toggle()
[C#]
public void Toggle();
[C++]
public: void Toggle();
[JScript]
public function Toggle();

Remarks

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

Note   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.

Example

[Visual Basic, C#, C++] The following 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 assumes 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.

[Visual Basic] 
Private Sub treeView1_MouseDown(sender As Object, _
  e As MouseEventArgs) Handles treeView1.MouseDown
   Select Case e.Button
      ' Remove the TreeNode under the mouse cursor 
      ' if the right mouse button was clicked. 
      Case MouseButtons.Right
         treeView1.GetNodeAt(e.X, e.Y).Remove()
      
      ' Remove 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()
   End Select
End Sub

[C#] 
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
   switch(e.Button)
   {
      // Remove the TreeNode under the mouse cursor 
      // if the right mouse button was clicked. 
      case MouseButtons.Right:
         treeView1.GetNodeAt(e.X, e.Y).Remove();
         break;
      
      // Remove 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();
         break;
   }
}

[C++] 
private:
   void treeView1_MouseDown(Object* /*sender*/, MouseEventArgs* e) {
      switch(e->Button) {
         // Remove the TreeNode under the mouse cursor
         // if the right mouse button was clicked.
      case MouseButtons::Right:
         treeView1->GetNodeAt(e->X, e->Y)->Remove();
         break;

         // Remove 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();
         break;
      }
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

TreeNode Class | TreeNode Members | System.Windows.Forms Namespace | Expand | ExpandAll | Collapse | IsExpanded

Show: