articles

Home / DeveloperSection / Articles / How to use Tree View Control in C#

How to use Tree View Control in C#

Anonymous User20621 27-Jan-2011

Windows forms Tree View control helps to represent data in hierarchical format. Whenever you want to display data in the format of parent child relationship or in hierarchical format then we can use concept of tree view control. The best example of tree view control is to show hierarchy of company, windows file system etc.

In next few paragraph am demonstrating you the use of tree view control. I had made a small program which explains certain functionality of tree view controls such as how to add parent node dynamically, how to add child node dynamically, how to delete any selected node.

Example to demonstrate use of Tree View Control

How to use Tree View Control in C#

In this example I had taken two panel control, one tree view control, one text box control and three button control. I had added tree view control at first panel and add group of textbox and buttons in second panel. Add Parent Node button is used to add Parent Node and Add Child Node Button is used to add child Node to a specific parent node while Delete node button is used to delete selected node. In textbox we enter the name of the node which is further added as a child or parent node.

Adding Parent Node in Tree View Control

For adding parent node enter the name of the node in a textbox and click Add Parent Node button.

How to use Tree View Control in C#

Code to Add Parent Node
privatevoid button1_Click(object sender,  EventArgs e)
{
            if (textBox1.Text.Trim().Length != 0) //if textbox is not empty.
            {
                TreeNode parentNode = newTreeNode(textBox1.Text); //create an object of //Tree Node class and pass node name to the constructor of Tree Node.
                treeView1.Nodes.Add(parentNode); //Adding parent node to a tree view.
                textBox1.Clear();//Clear the text box control.
            }
            else
            {//If textbox is empty the message box appears to show a message to enter //value in textbox control.
                MessageBox.Show("Please Enter Value In The TextBox.", "Data Entry Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
            }
}
Adding Child Node in Tree View Control

For adding any child node in a tree view control firstly we have to select the parent node for that child node. Then enter the name of the child node in a text box control and then click Add Child Node Button.How to use Tree View Control in C#

Code to Add Child Node
privatevoid button2_Click(object sender,  EventArgs e)
{
       if (treeView1.SelectedNode != null)//Check parent node is selected or not
            {
                if (textBox1.Text.Length != 0)//If child node name is entered.
                {
                    TreeNode childNode = newTreeNode(textBox1.Text);//Create an object of the child node and pass child node name to the constructor of Tree Node
                    treeView1.SelectedNode.Nodes.Add(childNode);//Add child node to the selected parent node
                    treeView1.ExpandAll();//Expand all the tree view control
                    textBox1.Clear();//clear the text box control.
                }
                else
                {//show the message to enter value in text box control if text box control is empty.
                    MessageBox.Show("Please Enter Value In The TextBox.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {//show the message to select node to which child node should be added.
                MessageBox.Show("Please Select Parent Node.", "Warning Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
}
Deleting Node in Tree View Control

To delete any node in tree view control firstly you have to select a node which needs to be deleted and then press Delete Node button.

How to use Tree View Control in C#

Code to Delete Child Node
privatevoid button3_Click(object sender,  EventArgs e)
{
            if (treeView1.Nodes.Count > 0)//Check whether tree view contains any node or //not.
            {
                if (treeView1.SelectedNode != null)//Check whether any node in tree view //control is selected or not.
                {
                    treeView1.SelectedNode.Remove();//If node is selected the remove that //node.
                    MessageBox.Show("Node Removed Successfully", "Success Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);//Show the success message to //that node.
                }
                else
                {//else show message to select any node.
                    MessageBox.Show("Please Select Any Node To Be Deleted.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                }
            }
            else
            {//else show message that tree view control is empty.
                MessageBox.Show("There Is No Node In The Tree View Control.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            }
}



Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By