Add ContextMenu to treeview nodes in c#

 
In this article we will learn that how to add contextmenu or contextmenustrip in treeview nodes at design time as well as runtime using c# programming language.

[wpfilebase tag=file id=4]
This example requires a windows form with TreeView control named ‘TreeView1’ and two contextmenustrips with name ‘ContextMenuStrip1’ and ‘ContextMenuStrip2’.
you can drag and drop both ContextmenuStrips from the ToolBox at design time (contextmenustrip one is for ContextMenuStrip1 and ContextMenuStrip2).

ContextmenuStrip1′ has two options:
(a) Add Root node ( we can create a new root Node)
(b) ClearAll (We can clear all nodes)

ContextmenuStrip2′ has three options:
(a) Add Child node (We can create a child node)
(b) Rename ( we can rename selected node)
(c) Delete (We can delete selected node)

Show contextmenu on treeview

Now we set ContextMenuStrip1 to treeview1 and ContextMenuStrip2 to treeview1’s nodes like as:

private void Form1_Load(System.Object sender, System.EventArgs e)
{
	TreeView1.ContextMenuStrip = ContextMenuStrip1;
	foreach (TreeNode RootNode in TreeView1.Nodes) {
		RootNode.ContextMenuStrip = ContextMenuStrip2;
		foreach (TreeNode ChildNode in RootNode.Nodes) {
			ChildNode.ContextMenuStrip = ContextMenuStrip2;
		}
	}
}

Contextmenu on treeview

Add a new windows form named Form1 with one textbox named ‘txtNodeName’. you can enter a node text for creating a new node to TreeView1

Code for adding new root:

private void AddNewRootToolStripMenuItem_Click(System.Object sender, System.EventArgs e)
{
	frmEnterText frm = new frmEnterText();
	frm.ShowDialog();
	string TempNodeText = frm.txtNodeName.Text;
	frm.Dispose();
	if (!string.IsNullOrEmpty(TempNodeText.Trim())) {
		TreeNode _Node = new TreeNode();
		_Node.Text = TempNodeText;
		_Node.ContextMenuStrip = ContextMenuStrip2;
		TreeView1.Nodes.Add(_Node);
	}
}

To Clear All nodes of TreeView1:

private void ClearAllToolStripMenuItem_Click(System.Object sender, System.EventArgs e)
{
	TreeView1.Nodes.Clear();
}

To create child node:

private void AddChildRootToolStripMenuItem_Click(System.Object sender, System.EventArgs e)
{
	frmEnterText frm = new frmEnterText();
	frm.ShowDialog();
	string TempNodeText = frm.txtNodeName.Text;
	frm.Dispose();
	if (!string.IsNullOrEmpty(TempNodeText.Trim())) {
		TreeNode _Node = new TreeNode();
		_Node.Text = TempNodeText;
		_Node.ContextMenuStrip = ContextMenuStrip2;
		TreeView1.SelectedNode.Nodes.Add(_Node);
	}
}

To Rename selected node:

private void RenameToolStripMenuItem_Click(System.Object sender, System.EventArgs e)
{
	frmEnterText frm = new frmEnterText();
	frm.ShowDialog();
	string TempNodeText = frm.txtNodeName.Text;
	frm.Dispose();
	TreeNode SelectedNode = TreeView1.SelectedNode;
	if (!string.IsNullOrEmpty(TempNodeText.Trim())) {
		SelectedNode.Text = TempNodeText;
	}
}

To Delete selected node:

private void DeleteToolStripMenuItem_Click(System.Object sender, System.EventArgs e)
{
	TreeView1.SelectedNode.Remove();
}