CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C# – How to use TreeView control in C# Windows Application?

TreeView control in C# is used to display the items in hierarchical form. Each item in TreeView control is called a node. A node can have sub-nodes too; and each sub-node has it’s own nodes, and so on. All the nodes in the TreeView control are displayed in an hierarchical form, for better readability and control.

Through this article, we are going to discuss, using TreeView control in C# Windows applications. We will discuss, adding and removing nodes in TreeView control.

Add TreeView control to the Form

Create a C# Windows Forms Application in Visual Studio IDE. In Form’s design view, add the TreeView control from the Toolbox. Visual Studio automatically, creates a variable for this control and by using this we can use the features of the control.

You can add the nodes to the control in design view also, by selecting Nodes property from the Properties dialog. Through this Article, we are going to discuss the methods used to manage the control.

Adding nodes to the TreeView control

A Treeview control is the collection of nodes; each node is in the same level or in different hierarchical position in the tree structure. These collection of nodes are managed through it’s Nodes property.

Nodes is of TreeNodeCollection type, that represents the collection of tree nodes assigned to the TreeView control.

TreeNodeCollection has Add() method, which allows to add a new tree node to the end of the Nodes collection. Add() method has number of variances; these are used to add a node with label text, with key, with image index etc,.

Usually, we use the simple Add() method variance to add a new tree node with labeled text. Here is the code in C#;

TreeNode node = ctrlTreeView.Nodes.Add("Parent Node");

The above statement adds a new tree node, and returns the tree node that has added to the tree Nodes collection. The node is of TreeNode type.

We have another variance of Add() method, which takes a key and a label as arguments to add a new tree node. This method is more useful than the previous one. Key is used to search for the node, and it is assigned to TreeNode‘s Name property. And the label will be displayed as the TreeNode‘s Text. By adding, key to the node; enables to easily figure out the node from the Nodes collection. Below is an example, which adds a node with key & label. As discussed, label will be displayed, and key will be set to it’s Name property;

TreeNode node = ctrlTreeView.Nodes.Add("Parent", "Parent Node");

Usually, it is good to give unique key values; to easily figure out the tree node from the collection of Nodes.

Adding sub-nodes to the TreeView control

When we add the node to the TreeView control; it returns the tree node that being added to the collection. By using the tree node, we can add sub-nodes to it. As I mentioned above, each tree node is of TreeNode type.

TreeNode type also has Nodes property (of TreeNodeCollection type) to manage the collection of nodes. Hence, we can use the same way (as above), to add the sub-nodes to the TreeNode. Below code adds the sub-node to the tree node; we got the tree node, from above statement; 

TreeNode sub_node = node.Nodes.Add("Child-1", "Sub Node");

This way, we can add nodes & sub-nodes in different hierarchical level in TreeView control.

Removing a node from TreeView control

We can remove the node from tree nodes collection, by using it’s index or key; or different other ways. As we are using key, while adding the nodes; we will start using the method to removing the nodes using key value (its’ Name property value).

TreeNodeCollection class provides a RemoveByKey() method to remove the node by key value; that means by using TreeNode.Name property.

node.Nodes.RemoveByKey("Child-1");

The above code, removes the child node, what we have added in the previous section; from the tree nodes collection.

We can also remove the node from tree nodes collection, if we have the reference to TreeNodeAs we have the reference of “Sub Node” in sub_node variable, below statement can directly removes the node from the collection.

node.Nodes.Remove(sub_node);

These methods will remove the node which is in current nodes collection.

We can also remove the node by calling it’s Remove() method. This is possible, only when we have the node reference.

sub_node.Remove();

Find a node in TreeView control

To find a node by it’s key, we use TreeNodeCollection‘s Find() method. We need to specify the key value and whether to look into it’s sub-nodes also to search for the node.

Find() method returns the list of nodes which matches the given key value. Below is an example, which removes the first node which matches with the given key value.

node.Nodes.Find("key-value", true)[0].Remove();

Here is the complete working code, demonstrates adding and removing nodes from TreeView control;

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace TreeView_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            // Display the text of selected node
            lblDisplay.Text = e.Node.Text;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Add parent node
            TreeNode node = treeViewControl.Nodes.Add("root", "Government Space Agencies");

            // Add child node
            TreeNode sub_node = node.Nodes.Add("country", "Country");

            // Add sub nodes
            sub_node.Nodes.Add("usa", "United States").Nodes.Add("nasa", "National Aeronautics and Space Administration (NASA)");
            sub_node.Nodes.Add("chn", "China").Nodes.Add("cnsa", "China National Space Administration (CNSA)");
            sub_node.Nodes.Add("jpn", "Japan").Nodes.Add("jaxa", "Japan Aerospace Exploration Agency (JAXA)");
            sub_node.Nodes.Add("ind", "India").Nodes.Add("isro", "Indian Space Research Organization (ISRO)");
            sub_node.Nodes.Add("rus", "Russia").Nodes.Add("rfsa", "Russian Federal Space Agency (RFSA)");

            sub_node.Nodes.Add("dummy-node", "Dummy Node");

            // -- Remove dummy node(s)

            // This will not remove the dummy-node, because it is not in this Nodes collection
            treeViewControl.Nodes.RemoveByKey("dummy-node");

            // Find for the dummy node & remove it
            treeViewControl.Nodes.Find("dummy-node", true)[0].Remove();

            // Expand all nodes
            treeViewControl.ExpandAll();
        }
    }
}

The screenshot of it, looks like below;

TreeView control demo
TreeView control demo

(Raju)

C# – How to use TreeView control in C# Windows Application?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top