- This topic shows how to add icon to TreeView nodes using C# programming.
- Tools needed: Button1 (Button1), TreeView1 (TreeView1).
TreeView Control – How to add icon to TreeView using C#
- The Windows Forms TreeView control displays a hierarchy of nodes, like the way files and folders are displayed in the left pane of the Windows Explorer feature in Windows operating systems.
- The top level of the TreeView is called Root node that can be expanded or collapsed if they have a child node
ImageList Class – How to add icon to TreeView using C#
- ImageList class provides methods to manage a collection of Image objects. This class cannot be inherited.
- Syntax
|
1 2 3 |
// Declaration public sealed class ImageList : Component { } |
ImageList.Images Property – How to add icon to TreeView using C#
- Helps to get the ImageList.ImageCollection for this image list.
- Syntax
|
1 2 3 |
// Declaration public ImageList.ImageCollection Images { } |
TreeView.ImageList Property – How to add icon to TreeView using C#
- Helps to assign the ImageList that contains the Image objects that are used by the tree nodes.
- Syntax
|
1 2 3 |
// Declaration public ImageList ImageList { } |
TreeView.SelectedImageIndex Property – How to add icon to TreeView using C#
- It helps to assign the image list index value of the image that is displayed when a tree node is selected.
- Syntax
|
1 2 3 4 |
// Declaration [RelatedImageListAttribute("ImageList")] public int SelectedImageIndex { } |
How to add icon to TreeView using C# – Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
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; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void FillTreeView() { // Load the images in an ImageList. ImageList ImageList = new ImageList(); ImageList.Images.Add(Image.FromFile("C:\\Winter.jpg")); ImageList.Images.Add(Image.FromFile("C:\\testimage.jpg")); ImageList.Images.Add(Image.FromFile("C:\\sunset.jpg")); // Assign the ImageList to the TreeView. TreeView1.ImageList = ImageList; } private void Form1_Load(object sender, EventArgs e) { FillTreeView(); // Set the TreeView control's default image and selected image indexes. TreeView1.ImageIndex = 0; TreeView1.SelectedImageIndex = 1; TreeView1.Nodes.Add("Class Room"); TreeView1.Nodes[0].Nodes.Add("STD I"); TreeView1.Nodes[0].Nodes[0].Nodes.Add("DIV A"); TreeView1.Nodes[0].Nodes.Add("STD II"); TreeView1.Nodes[0].Nodes[1].Nodes.Add("DIV B"); TreeView1.Nodes[0].Nodes.Add("STD III"); TreeView1.Nodes[0].Nodes[2].Nodes.Add("DIV C"); } } } |
| TreeView controls in C# |
| TreeView Controls |
How to add icon to TreeView using C#



