We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: TreeViewAdv

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!

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile