SIGN UP MEMBER LOGIN:    
ARTICLE

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

Posted by Ghanashyam Nayak Articles | Visual C# April 24, 2011
This article will show how to add a Root Node & Child Node to a TreeView selected node at runtime & the user can rename the node by pressing the "F2' key from the keyboard...
Reader Level:


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 that 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;
        }
    }
}

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor