ComponentOne TreeView for WinForms
Adding and Removing Nodes
Nodes > Adding and Removing Nodes

Adding Nodes

The C1TreeNodeCollection class provides the Add method and the Insert method for adding nodes in TreeView. To add a parent node, you can use either of the two methods with the Nodes collection of the C1TreeView class. Next, to add a child node in the parent node, or children nodes in a child node, you can either use the Add method or the Insert method with the Nodes collection of the C1TreeNode class.

The following code snippet demonstrates how to add nodes in the TreeView control using the Add method.

' create parent nodes
Dim parentNode1 As New C1.Win.TreeView.C1TreeNode()
Dim parentNode2 As New C1.Win.TreeView.C1TreeNode()

' add the parent nodes to the TreeView Nodes collection
C1TreeView1.Nodes.Add(parentNode1)
C1TreeView1.Nodes.Add(parentNode2)

' set the values of the parent nodes
parentNode1.SetValue("Parent1", 0)
parentNode2.SetValue("Parent2", 0)

' create child nodes
Dim childNode1 As New C1.Win.TreeView.C1TreeNode()
Dim childNode2 As New C1.Win.TreeView.C1TreeNode()

' add the child nodes to the parent nodes' Nodes collection
parentNode1.Nodes.Add(childNode1)
parentNode2.Nodes.Add(childNode2)

' set the values of the child nodes
childNode1.SetValue("Child1", 0)
childNode2.SetValue("Child2", 0)
// create parent nodes
C1.Win.TreeView.C1TreeNode parentNode1 = new C1.Win.TreeView.C1TreeNode();
C1.Win.TreeView.C1TreeNode parentNode2 = new C1.Win.TreeView.C1TreeNode();

// add the parent nodes to the TreeView Nodes collection
c1TreeView1.Nodes.Add(parentNode1);
c1TreeView1.Nodes.Add(parentNode2);

// set the values of the parent nodes
parentNode1.SetValue("Parent1", 0);
parentNode2.SetValue("Parent2", 0);

// create child nodes
C1.Win.TreeView.C1TreeNode childNode1 = new C1.Win.TreeView.C1TreeNode();
C1.Win.TreeView.C1TreeNode childNode2 = new C1.Win.TreeView.C1TreeNode();

// add the child nodes to the parent nodes' Nodes collection
parentNode1.Nodes.Add(childNode1);
parentNode2.Nodes.Add(childNode2);

// set the values of the child nodes
childNode1.SetValue("Child1", 0);
childNode2.SetValue("Child2", 0);

The following image shows the treeview after adding nodes.

The following code snippet demonstrates how to add nodes in the TreeView control using the Insert method.

' create third parent node
Dim parentNode3 As New C1.Win.TreeView.C1TreeNode()

' insert the third parent node in the TreeView Nodes collection
C1TreeView1.Nodes.Insert(2, parentNode3)

' set the value of the third parent node
parentNode3.SetValue("Parent3", 0)

' create third child node
Dim childNode3 As New C1.Win.TreeView.C1TreeNode()

' insert the third child node in the third parent node's Nodes collection
parentNode3.Nodes.Insert(0, childNode3)

' set the value of the third child node
childNode3.SetValue("Child3", 0)
// create third parent node
C1.Win.TreeView.C1TreeNode parentNode3 = new C1.Win.TreeView.C1TreeNode();

// insert the third parent node in the TreeView Nodes collection
c1TreeView1.Nodes.Insert(2, parentNode3);

// set the value of the third parent node
parentNode3.SetValue("Parent3", 0);
    
// create third child node
C1.Win.TreeView.C1TreeNode childNode3 = new C1.Win.TreeView.C1TreeNode();

// insert the third child node in the third parent node's Nodes collection
parentNode3.Nodes.Insert(0, childNode3);

// set the value of the third child node
childNode3.SetValue("Child3", 0);

The final treeview appears, as shown below.

Removing Nodes

The C1TreeNodeCollection class provides the Remove method and the RemoveAt method for removing nodes from TreeView. You can use either of the two methods with the Nodes collection of C1TreeView to remove parent nodes from TreeView. To remove a child node from a parent node or children nodes from a child node, you can use either of the two methods with the Nodes collection of C1TreeNode.

The Remove method and the RemoveAt method removes a single node at a time. However, to remove all nodes simultaneously from the Nodes collection of TreeView, a parent node, or a child node, you can use the Clear method with the Nodes collection of C1TreeView or C1TreeNode, respectively.

Here is the code snippet demonstrating how to set these methods.

' remove the second parent node from the TreeView Nodes collection
C1TreeView1.Nodes.Remove(parentNode2)

' remove the node at index 1
C1TreeView1.Nodes.RemoveAt(1)
// remove the second parent node from the TreeView Nodes collection
c1TreeView1.Nodes.Remove(parentNode2);

// remove the node at index 1
c1TreeView1.Nodes.RemoveAt(1);

 After removing the nodes, the treeview appears like the following.