TreeNodeCollection.Remove Method (TreeNode)

 

Removes the specified tree node from the tree node collection.

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

public void Remove(
	TreeNode node
)

Parameters

node
Type: System.Windows.Forms.TreeNode

The TreeNode to remove.

When a TreeNode is removed from the tree node collection, all subsequent tree nodes are moved up one position in the collection.

You can also remove a TreeNode that you previously added by using the RemoveAt or Clear methods.

System_CAPS_noteNote

Enumerating the collection and removing nodes is not supported.

To add new TreeNode objects to the collection, use the Add, AddRange, or Insert methods.

The following code example removes the selected tree node from one TreeView and adds it to another if both tree node collections are not read-only. When a Button is clicked, the TreeNode represented by the TreeView.SelectedNode property is deleted from one TreeView using the Remove method and added to the other TreeView using the Insert method. This example requires that you have a Form that contains two TreeView controls and a Button. The TreeView controls should be named treeView1 and treeView2.

private void button1_Click(object sender, EventArgs e)
{
   // If neither TreeNodeCollection is read-only, move the 
   // selected node from treeView1 to treeView2.
   if(!treeView1.Nodes.IsReadOnly && !treeView2.Nodes.IsReadOnly)
   {
      if(treeView1.SelectedNode != null)
      {
         TreeNode tn = treeView1.SelectedNode;
         treeView1.Nodes.Remove(tn);
         treeView2.Nodes.Insert(treeView2.Nodes.Count, tn);
      }
   }
}

.NET Framework
Available since 1.1
Return to top
Show: