C# TreeView

How to use TreeView control in C#?

This C# TreeView tutorial describes the TreeView control in Windows Forms.



C# TreeViev screenshot from code below.

First you need to create a TreeView and ImageList, in ImageList set pictures and you can start programming the tree:



using System;
using System.Drawing;
using System.Windows.Forms;

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

        
private void Form1_Load(object sender, EventArgs e)
        {
            
TreeNode ParentNode = new TreeNode();
            ParentNode.Text = 
"RootNode";
            ParentNode.ForeColor = 
Color.Black;
            ParentNode.BackColor = 
Color.White;
            ParentNode.ImageIndex = 0;
            ParentNode.SelectedImageIndex = 0;
            treeView1.Nodes.Add(ParentNode);           
//Root node in TreeView

            
TreeNode ChildNode1 = new TreeNode();
            ChildNode1.Text = 
"Child 1";
            ChildNode1.ForeColor = 
Color.Black;
            ChildNode1.BackColor = 
Color.White;
            ChildNode1.ImageIndex = 0;
            ChildNode1.SelectedImageIndex = 0;
            ParentNode.Nodes.Add(ChildNode1);         
//Child node (parent is ParentNode)

            
TreeNode ChildNode2 = new TreeNode();
            ChildNode2.Text = 
"Child 2";
            ChildNode2.ForeColor = 
Color.Black;
            ChildNode2.BackColor = 
Color.White;
            ChildNode2.ImageIndex = 0;
            ChildNode2.SelectedImageIndex = 0;
            ParentNode.Nodes.Add(ChildNode2);         
//Child node (parent is ParentNode)

            
TreeNode ChildNode3 = new TreeNode();
            ChildNode3.Text = 
"Child 3";
            ChildNode3.ForeColor = 
Color.Black;
            ChildNode3.BackColor = 
Color.White;
            ChildNode3.ImageIndex = 0;
            ChildNode3.SelectedImageIndex = 0;
            ParentNode.Nodes.Add(ChildNode3);         
//Child node (parent is ParentNode)

            
TreeNode ChildNode4 = new TreeNode();
            ChildNode4.Text = 
"Child 4";
            ChildNode4.ForeColor = 
Color.Black;
            ChildNode4.BackColor = 
Color.White;
            ChildNode4.ImageIndex = 0;
            ChildNode4.SelectedImageIndex = 0;
            ParentNode.Nodes.Add(ChildNode4);         
//Child node (parent is ParentNode)

            
TreeNode NextChildNode1 = new TreeNode();
            NextChildNode1.Text = 
"Next Child 1";
            NextChildNode1.ForeColor = 
Color.Black;
            NextChildNode1.BackColor = 
Color.White;
            NextChildNode1.ImageIndex = 1;
            NextChildNode1.SelectedImageIndex = 1;
            ChildNode3.Nodes.Add(NextChildNode1);         
//Child node (parent is ChildNode3)

            
TreeNode NextChildNode2 = new TreeNode();
            NextChildNode2.Text = 
"Next Child 2";
            NextChildNode2.ForeColor = 
Color.Black;
            NextChildNode2.BackColor = 
Color.White;
            NextChildNode2.ImageIndex = 1;
            NextChildNode2.SelectedImageIndex = 1;
            ChildNode3.Nodes.Add(NextChildNode2);         
//Child node (parent is ChildNode3)

            treeView1.ExpandAll();
        }
    }
}


It's easy. Good luck with TreeView control.



C# TreeView - How to add node to selected node

C# TreeView - How to remove node

C# TreeView - How to prevent nodes from collapsing

C# TreeView CheckBoxes

C# TreeView - Get all Child Nodes