Export (0) Print
Expand All

TreeNodeCollection.Remove Method

Removes the specified tree node from the tree node collection.

[Visual Basic]
Public Sub Remove( _
   ByVal node As TreeNode _
)
[C#]
public void Remove(
 TreeNode node
);
[C++]
public: void Remove(
 TreeNode* node
);
[JScript]
public function Remove(
   node : TreeNode
);

Parameters

node
The TreeNode to remove.

Remarks

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.

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

Example

[Visual Basic, C#, C++] The following 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 assumes you have a Form that contains two TreeView controls and a Button. The TreeView controls should be named treeView1 and treeView2.

[Visual Basic] 
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
   ' If neither TreeNodeCollection is read-only, move the 
   ' selected node from treeView1 to treeView2. 
   If Not treeView1.Nodes.IsReadOnly And Not treeView2.Nodes.IsReadOnly Then
      If Not (treeView1.SelectedNode Is Nothing) Then
         Dim tn As TreeNode = treeView1.SelectedNode
         treeView1.Nodes.Remove(tn)
         treeView2.Nodes.Insert(treeView2.Nodes.Count, tn)
      End If
   End If
End Sub

[C#] 
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);
      }
   }
}

[C++] 
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 != 0) {
                TreeNode* tn = treeView1->SelectedNode;
                treeView1->Nodes->Remove(tn);
                treeView2->Nodes->Insert(treeView2->Nodes->Count, tn);
            }
        }
    }

[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

TreeNodeCollection Class | TreeNodeCollection Members | System.Windows.Forms Namespace | RemoveAt

Show:
© 2014 Microsoft