$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
Windows Forms KetticTreeView Control
Build complex navigation systems and display complex hierarchical structures
Home > WinForms UI Controls > User Manual > Adding and Removing Nodes in C#

Adding/Removing Nodes of KetticTreeView in C#.NET

The nodes commands of KetticTreeView are placed in the button bar on the left panel of the property builder. These node commands allow us to add, remove, and arrange the nodes of the tree view structure. For example, we can easily add a new root node or a child node, as well as remove the selected node. At the same time, we can make the chosen child node a sibling of its parent and make the chosen node a child of the previous node in the list. Furthermore, we are able to move the selected node up and down in the list. The following C# tutorial demonstrates how to manipulate the nodes at run time.

C# Add Nodes to KetticTreeView control

The nodes of KetticTreeView are in a hierarchical structure. The KetticTreeView includes the Nodes.Add() method for the developers to create a node to the KetticTreeView. When we need to create a descendant of the node, we can use the parent nodes Nodes.Add() method. The following C# code snippet demonstrates how to add nodes to KetticTreeView.

private void AddNodes()
{
KetticTreeNode Node1 = new KetticTreeNode("Node1");
Node1.Tag = 12;
Node1.BackColor = Color.Blue;
KetticTreeNode Node2 = new KetticTreeNode("Node2");
ketticTreeView1.Nodes.Add(Node1);
Node1.Nodes.Add(Node2);
}
If we need to cancel the node adding before it is added, we can use the NodeAdding event, which is fired before adding a node. When we shall cancel the added node, we can use the NodeAdded event, which is fired when the node is added successfully. The following C# code sample shows how to using the events.

void ketticTreeView1_NodeAdding(object sender, KetticTreeViewCancelEventArgs e)
{
if (e.Node.Text.Contains("Non-insertable"))
{
e.Cancel = true;
}
}

void ketticTreeView1_NodeAdded(object sender, KetticTreeViewEventArgs e)
{
KetticMessageBox.Show("Node {" + e.Node.Text + "} was added");
}

C# Remove Nodes to KetticTreeView control

KetticTreeView contains the Remove() method to delete an individual node and the Nodes.Clear() method to delete the entire nodes at once. The C# code snippet below show how to use the two methods

<pre class="csharpcode">
<span class="kwrd">private</span> <span class="kwrd">void</span> RemoveNodes()
{
ketticTreeView1.Nodes[0].Remove();
ketticTreeView1.Nodes.Clear();
}
</pre>
If we need to cancel the node removing before it is deleted, we can use the NodeRemoving event, which is fired before removing a node. When we shall cancel the removed node, we can use the NodeRemoved event, which is fired when the node is removed successfully. The following C# code sample shows how to using the events.

void ketticTreeView1_NodeRemoving(object sender, KetticTreeViewCancelEventArgs e)
{
if (e.Node.Text.Contains("Unremovable"))
{
e.Cancel = true;
}
}

void ketticTreeView1_NodeRemoved(object sender, KetticTreeViewEventArgs e)
{
KetticMessageBox.Show("Node {" + e.Node.Text + "} was removed");
}
UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls