New to Telerik UI for WinForms? Download free 30-day trial

Adding and Removing Nodes

Adding Nodes

RadTreeView nodes are arranged in a hierarchical structure. To add a node to the first level in a RadTreeView use the Nodes.Add() method. To add a child node to another node use the parent nodes Nodes.Add() method. Nodes.Add() places the node at the end of the Nodes collection. You may also use Nodes.Insert() to place the node anywhere in the Nodes collection.

The following example demonstrates how to add nodes using code:

private void AddNodes()
{
    RadTreeNode Node1 = new RadTreeNode("Node1");
    Node1.Tag = 1234;
    Node1.BackColor = Color.Blue;
    RadTreeNode Node2 = new RadTreeNode("Node2");
    RadTreeNode Node3 = new RadTreeNode("Node3");
    RadTreeNode Node4 = new RadTreeNode("Node4");
    radTreeView1.Nodes.Add(Node1);
    radTreeView1.Nodes.Add(Node2);
    Node1.Nodes.Add(Node3);
    Node2.Nodes.Add(Node4);
    //Alternative methods for adding nodes
    //RadTreeNode Node1 = radTreeView1.Nodes.Add("Node1");
    //RadTreeNode Node2 = radTreeView1.Nodes.Add("Node2");
    //Node1.Nodes.Add("Node3");
    //Node2.Nodes.Add("Node4");
}

Private Sub AddNodes()
    Dim Node1 As New RadTreeNode("Node1")
    Node1.Tag = 1234
    Node1.BackColor = Color.Blue
    Dim Node2 As New RadTreeNode("Node2")
    Dim Node3 As New RadTreeNode("Node3")
    Dim Node4 As New RadTreeNode("Node4")
    RadTreeView1.Nodes.Add(Node1)
    RadTreeView1.Nodes.Add(Node2)
    Node1.Nodes.Add(Node3)
    Node2.Nodes.Add(Node4)
    'Alternative methods for adding nodes
    'RadTreeNode Node1 = radTreeView1.Nodes.Add("Node1");
    'RadTreeNode Node2 = radTreeView1.Nodes.Add("Node2");
    'Node1.Nodes.Add("Node3");
    'Node2.Nodes.Add("Node4");
End Sub

Just before a node is added, the NodeAdding event is fired. The event arguments of this event allow you to cancel the adding of the node. If the operation was not canceled, then the node will be added to the RadTreeView and the NodeAdded event will be fired. The following code snippet demonstrates the usage of these events:

void radTreeView1_NodeAdding(object sender, RadTreeViewCancelEventArgs e)
{
    if (e.Node.Text.Contains("Non-insertable"))
    {
        e.Cancel = true;
    }
}
void radTreeView1_NodeAdded(object sender, RadTreeViewEventArgs e)
{
    RadMessageBox.Show("Node {" + e.Node.Text + "} was added");
}

Private Sub RadTreeView1_NodeAdding(sender As Object, e As RadTreeViewCancelEventArgs) Handles RadTreeView1.NodeAdding
    If e.Node.Text.Contains("Non-insertable") Then
        e.Cancel = True
    End If
End Sub
Private Sub RadTreeView1_NodeAdded(sender As Object, e As RadTreeViewEventArgs) Handles RadTreeView1.NodeAdded
    RadMessageBox.Show("Node {" + e.Node.Text + "} was added")
End Sub

Removing Nodes

To remove a single node use the Remove() method of the node. To remove all nodes of the RadTreeView use the Nodes.Clear() method.

private void RemoveNodes()
{
    //Remove a single node
    radTreeView1.Nodes[0].Remove();
    // removes all nodes from TreeView
    radTreeView1.Nodes.Clear();
}

Private Sub RemoveNodes()
    'Remove a single node
    RadTreeView1.Nodes(0).Remove()
    ' removes all nodes from TreeView
    RadTreeView1.Nodes.Clear()
End Sub

Just before a node is removed, the NodeRemoving event is fired. The event arguments of this event allow you to cancel the remove operation. If the operation was not canceled, then the node will be removed from RadTreeView and the NodeRemoved event will be fired. The following code snippet demonstrates the usage of these events:

void radTreeView1_NodeRemoving(object sender, RadTreeViewCancelEventArgs e)
{
    if (e.Node.Text.Contains("Unremovable"))
    {
        e.Cancel = true;
    }
}
void radTreeView1_NodeRemoved(object sender, RadTreeViewEventArgs e)
{
    RadMessageBox.Show("Node {" + e.Node.Text + "} was removed");
}

Private Sub RadTreeView1_NodeRemoving(sender As Object, e As RadTreeViewCancelEventArgs) Handles RadTreeView1.NodeRemoving
    If e.Node.Text.Contains("Unremovable") Then
        e.Cancel = True
    End If
End Sub
Private Sub RadTreeView1_NodeRemoved(sender As Object, e As RadTreeViewEventArgs) Handles RadTreeView1.NodeRemoved
    RadMessageBox.Show("Node {" + e.Node.Text + "} was removed")
End Sub

See Also

In this article