Articles in this section
Category / Section

How to set the images for treeview nodes in the WinForms TreeView?

2 mins read

WinForms TreeView control can be customized with images to the Left or Right side of its TreeNodeAdv. The images can be set on the Left or Right by using the properties LeftImageList and RightImageList. It is also possible to specify different images for the individual nodes by using the LeftImageIndices and RightImageIndices properties in the TreeNodeAdv.


C#

//Assigns image list to the TreeNodeAdv.
this.treeViewAdv1.LeftImageList = this.imageList1;
this.treeViewAdv1.RightImageList = this.imageList1;
 
//Gets the Path of the node and AddSeparatorAtEnd property set to true.
string path = e.Node.GetPath("\\");
 
//Gets an Array of Directories from the current directory path.
ArrayList dirs = new ArrayList(Directory.GetDirectories(path));
 
//Adds the Directories as a node in the TreeViewAdv
for (int i = 0; i < dirs.Count; i++)
{
    string dir = (string)dirs[i];
    int lastIndex = dir.LastIndexOf("\\") + 1;
    TreeNodeAdv node = new TreeNodeAdv(dir.Substring(lastIndex));
    e.Node.Nodes.Add(node);
    node.RightImageIndices = new int[] { 1 };
}
 
//Sets the Left images for the TreeNodeAdv.
foreach (TreeNodeAdv node in this.treeViewAdv1.Nodes[0].Nodes)
{
    node.LeftImageIndices = new int[] { 1 };
    node.RightImageIndices = new int[] { -1 };
}
 
//Sets the Right images for the TreeNodeAdv.
foreach (TreeNodeAdv node in this.treeViewAdv1.Nodes[0].Nodes)
{
    node.RightImageIndices = new int[] { 1 };
    node.LeftImageIndices = new int[] { -1 };
}

 

VB

'Assigns image list to the TreeNodeAdv.
Me.treeViewAdv1.LeftImageList = Me.imageList1
Me.treeViewAdv1.RightImageList = Me.imageList1
 
 
'Gets the Path of the node and AddSeparatorAtEnd property set to true.
Dim path As String = e.Node.GetPath("\")
 
'Gets an Array of Directories from the current directory path.
Dim dirs As New ArrayList(Directory.GetDirectories(path))
 
'Adds the Directories as a node in the TreeViewAdv.
For i As Integer = 0 To dirs.Count - 1
     Dim dir As String = CStr(dirs(i))
     Dim lastIndex As Integer = dir.LastIndexOf("\") + 1
     Dim node As New TreeNodeAdv(dir.Substring(lastIndex))
     e.Node.Nodes.Add(node)
     node.RightImageIndices = New Integer() { 1 }
Next i
 
'Sets the Left images for the TreeNodeAdv.
For Each node As TreeNodeAdv In Me.treeViewAdv1.Nodes(0).Nodes
     node.LeftImageIndices = New Integer() { 1 }
     node.RightImageIndices = New Integer() { -1 }
Next node
'Sets the Right images for the TreeNodeAdv.
 
For Each node As TreeNodeAdv In Me.treeViewAdv1.Nodes(0).Nodes
     node.RightImageIndices = New Integer() { 1 }
     node.LeftImageIndices = New Integer() { -1 }
Next node

 

Images aligned left side of the TreeNodeAdv

Figure 1: Images aligned on the left side of the TreeNodeAdv

Images aligned right side of the TreeNodeAdv

Figure 2: Images aligned right side of the TreeNodeAdv


View Sample in GitHub

Reference link: https://help.syncfusion.com/windowsforms/treeview/treenodeadvcustomization#image-customization.

 

 

Conclusion.

I hope you enjoyed learning about how to set the images for treeview nodes in the WinForms TreeView. You can explore runnable sample of set the images for treeview nodes from this GitHub location.

You can refer to our WinForms TreeView feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms TreeView example   to understand how to present and manipulate data.  

For current customers, you can check out our WinForms controls from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms Pivot Grid and other WinForms controls. 

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied