articles

Home / DeveloperSection / Articles / Adding and Removing Nodes in Treeview C#

Adding and Removing Nodes in Treeview C#

AVADHESH PATEL 30915 23-Feb-2013

TreeView nodes are arranged in a hierarchical structure. Here I have described how to insert nodes into treeview and how to delete them. Steps are given below

Step 1: First open windows application and create windows form as below, that functionality described later.

Adding and Removing Nodes in Treeview C#

Note: Bonded node is combobox control that contains all inserted (parent and child) node from textbox and items will be deleted from treeview and combobox when user selects any node for deletion.

After inserted node into treeview window look as following image.

Adding and Removing Nodes in Treeview C#

Step 2: Now generate “Parent” button click event and write down below line of code. This line of code used to add parent nodes in treeview.

private void btnParent_Click(object sender, EventArgs e)

        {
            if (textBox1.Text.Trim().Length != 0) //if textbox is not empty.
            {
                TreeNode parentNode = new TreeNode(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.
                comboBox1.Items.Add(textBox1.Text); // Add items into combobox
                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);
            }
        }

 Note: Add below line of code in “btnParent_Click” event. Below line of code contain list of selected node and child on node selection that used later for deleting from cambobox.


public List<string> sList = new List<string>();

  Step 3: Generate Child button click event and write down below line of code.

private void btnChild_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 = new TreeNode(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
                    comboBox1.Items.Add(textBox1.Text); // Add items into combobox
                    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);
            }
        }

  Step 4: Generate click event of “delete” button and used below line of code.

private void btnDelete_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.
                {
                    comboBox1.Items.Remove(treeView1.SelectedNode.Text);
                    treeView1.SelectedNode.Remove();//If node is selected the remove that //node.  
                    foreach (string str in sList) // Remove item from combobox
                    {
                        comboBox1.Items.Remove(str.ToString());
                    }
                    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);
            }
        }

  Step 5: Generate “NodeMouseClick” event of treeview and write down below line of code. This event gets all sub node name and store into list.

  

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            sList.Clear();
            CallNodesSelector(e);
        }

  Write down “CallNodesSelector” method description as below. This method call by


treeview “NodeMouseClick” event which described below.

 

private void CallNodesSelector(TreeNodeMouseClickEventArgs e)

        {
            TreeNodeCollection nodes = e.Node.Nodes;
            foreach (TreeNode n in nodes)
            {
                sList.Add(n.Text);
                GetNodeRecursive(n);
            }
        }
        private void GetNodeRecursive(TreeNode treeNode)
        {
            foreach (TreeNode tn in treeNode.Nodes)
            {
                sList.Add(tn.Text);
                GetNodeRecursive(tn);
            }
        }

  Demo finished hare. I hope this article is helpful you.


c# c# 
Updated 24-Feb-2020
Avadhesh Kumar Patel District Project Manager - Aligarh 14 months work experience in Panchayati Raj Department Sector as District Project Manager & 12 months work experience in IT Sector as Software Engineer. :-)

Leave Comment

Comments

Liked By