- This topic shows how to add icon to TreeView nodes using VB programming.
- Tools needed: Button1 (Button1), TreeView1 (TreeView1).
TreeView Control – How to add icon to TreeView using VB
- 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 VB
- ImageList class provides methods to manage a collection of Image objects. This class cannot be inherited.
- Syntax
|
1 2 3 |
'Declaration Public NotInheritable Class ImageList _ Inherits Component |
ImageList.Images Property – How to add icon to TreeView using VB
- Helps to get the ImageList.ImageCollection for this image list.
- Syntax
|
1 2 |
'Declaration Public ReadOnly Property Images As ImageList.ImageCollection |
TreeView.ImageList Property – How to add icon to TreeView using VB
- Helps to assign the ImageList that contains the Image objects that are used by the tree nodes.
- Syntax
|
1 2 |
'Declaration Public Property ImageList As ImageList |
TreeView.SelectedImageIndex Property – How to add icon to TreeView using VB
- 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 |
'Declaration <RelatedImageListAttribute("ImageList")> _ Public Property SelectedImageIndex As Integer |
How to add icon to TreeView using VB – 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 |
Public Class Form1 Private Sub FillTreeView() ' Load the images in an ImageList. Dim ImageList As 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 End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 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") End Sub End Class |
How to add icon to TreeView using VB
| Working of TreeView in VB |
| TreeView control |
How to add icon to TreeView using VB



