Add Root/Child Node to a TreeView Selected Node At Runtime and Rename the Selected Node

In this article, new nodes for a TreeView are added at runtime.

It will add a value specified by the user into the TreeView as a root node or as a child node for a selected node.

In this article, I am also using another concept which is that when the user presses "F2" after selecting a node then the user can rename that node at runtime & after that set new value to that selected node.

The output when run the first time:

RootNode1.JPG

First of all we see how you can add a user's value as the Root Node into your TreeView?

MyTreeView.Nodes.Add(txtAddRootNode.Text);

Here "Nodes.Add" will add the value as the Root Node:

RootNode2.JPG

After that we see how you can add a child node to a particular selected node:

MyTreeView.SelectedNode.Nodes.Add(txtAddChildNode.Text);

Here it adds the value as a Child Node to the Selected Node:

RootNode3.JPG

After that how will you identify whether user pressed the "F2" key or not?

e.KeyCode.Equals(Keys.F2)

Here it checks the "KeyCode" with "Keys.F2"; if the user presses "F2" from the keyboard then it will match and return true.

The main question is in which event can we check the user's key?

We can using the "KeyDown" event; here I use this event for TreeView because I wanna allow user to rename the Selected Node using The "F2" key.

What is purpose of the "MyTreeView.LabelEdit = true;" line?

Using the above line you can allow the user to rename the selected node value at runtime.

And what is use of thge line "MyTreeView.SelectedNode.BeginEdit();"?

That line will initiate the editing of the selected node of the TreeView.

Here I set "MyTreeView.LabelEdit = false" , because I wanna allow only renaming feature. When the user presses "F2" and the LabelEdit property is not false then the user can rename the Selected Node on clicking the Node, so make it false then user can't rename the node until press "F2".

RootNode4.JPG

Main Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
 
namespace AddRootAndChildNodeAndRenameNode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent(); 
            MyTreeView.KeyDown+=new KeyEventHandler(MyTreeView_KeyDown);
            btnAddChildNode.Click+=new EventHandler(btnAddChildNode_Click);
            MyTreeView.Enter+=new EventHandler(MyTreeView_Enter);
            MyTreeView.Click += new EventHandler(MyTreeView_Click);
            MyTreeView.ExpandAll();
        }
 
  private void btnAddRootNode_Click(object sender, EventArgs e)
        {
            MyTreeView.Nodes.Add(txtAddRootNode.Text);
        }

        private void btnAddChildNode_Click(object sender, EventArgs e)
        {
            MyTreeView.SelectedNode.Nodes.Add(txtAddChildNode.Text);
        }

        private void MyTreeView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.F2))
            {
                MyTreeView.LabelEdit = true;
                MyTreeView.SelectedNode.BeginEdit();
            }
        }

        private void MyTreeView_Enter(object sender, EventArgs e)
        {
            MyTreeView.LabelEdit = false;
        }

        private void MyTreeView_Click(object sender, EventArgs e)
        {
            MyTreeView.LabelEdit = false;
        }
    }
}


Similar Articles