TreeNode.Remove Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Removes the current tree node from the tree view control.

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

public void Remove()

When the Remove method is called, the tree node, and any child tree nodes that are assigned to the TreeNode, are removed from the TreeView. The removed child nodes are removed from the TreeView but are still attached to this tree node.

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();

.NET Framework
Available since 1.1
Return to top