Add icon to a TreeView for beginner
To add an icon or an image to each node in a TreeView, you need to associate an ImageList. In this post, I will show you have to do it base from a code from a previous post I made. I will simply add the missing lines in it and highlight them in yellow. So I will print 2 codes: before and after.
It is important to note that the code use here is efficient for small TreeView. It won�t be a good idea if the TreeView has a lot of items. The problem is performance.
Before:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Me.TreeView1.Nodes.Add("MAIN") Dim no As TreeNode no = Me.TreeView1.Nodes.Item(0) findfiles(no, "C:\ProgramData\Microsoft\Windows\Start Menu", "*.lnk") End Sub Private Sub findfiles(no1 As TreeNode, Optional dirPath As String = "c:\temp\", Optional String_to_Search As String = "*.dll") Dim index1 As Integer Dim index2 As Integer Dim path_valid As Boolean path_valid = True '--------[some lines removes to simplify]----------------------- For Each foundFile As String In My.Computer.FileSystem.GetFiles( _ dirPath, FileIO.SearchOption.SearchTopLevelOnly, String_to_Search) Dim sFiles() As String sFiles = foundFile.Split("\") no1.Nodes.Add(sFiles(sFiles.Length - 1), sFiles(sFiles.Length � 1)) Next For Each foundDirectory As String In My.Computer.FileSystem.GetDirectories( dirPath, FileIO.SearchOption.SearchTopLevelOnly) Dim sDirectory() As String sDirectory = foundDirectory.Split("\") Dim no2 As TreeNode no2 = no1.Nodes.Add(sDirectory(sDirectory.Length - 1), _ sDirectory(sDirectory.Length � 1)) findfiles(no2, foundDirectory, String_to_Search) Next '--------[some lines removes to simplify]----------------------- End Sub |
After:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim oIcon As System.Drawing.Icon If System.IO.File.Exists("c:\Windows\explorer.exe") Then oIcon = System.Drawing.Icon.ExtractAssociatedIcon("c:\Windows\explorer.exe") End If ImageList1.Images.Add("MAIN", oIcon) Me.TreeView1.ImageList = ImageList1 Me.TreeView1.Nodes.Add("MAIN") Dim no As TreeNode no = Me.TreeView1.Nodes.Item(0) findfiles(no, "C:\ProgramData\Microsoft\Windows\Start Menu", "*.lnk") End Sub Private Sub findfiles(no1 As TreeNode, Optional dirPath As String = "c:\temp\", Optional String_to_Search As String = "*.dll") Dim index1 As Integer Dim index2 As Integer Dim path_valid As Boolean path_valid = True '--------[some lines removes to simplify]----------------------- For Each foundFile As String In My.Computer.FileSystem.GetFiles( _ dirPath, FileIO.SearchOption.SearchTopLevelOnly, String_to_Search) Dim sFiles() As String sFiles = foundFile.Split("\") Dim oIcon As System.Drawing.Icon If System.IO.File.Exists(foundFile) Then oIcon = System.Drawing.Icon.ExtractAssociatedIcon(foundFile) ImageList1.Images.Add(oIcon) End If no1.Nodes.Add(sFiles(sFiles.Length - 1), sFiles(sFiles.Length - 1), ImageList1.Images.Count) Next For Each foundDirectory As String In My.Computer.FileSystem.GetDirectories( dirPath, FileIO.SearchOption.SearchTopLevelOnly) Dim sDirectory() As String sDirectory = foundDirectory.Split("\") Dim no2 As TreeNode no2 = no1.Nodes.Add(sDirectory(sDirectory.Length - 1), _ sDirectory(sDirectory.Length - 1), 0) findfiles(no2, foundDirectory, String_to_Search) Next '--------[some lines removes to simplify]----------------------- End Sub |
The changes are in yellow. If I summarize the changes, each time the programs reads files, it extract and save the icon in the TreeNode. For the directories, it will always pick up the first icon from ImageList. That way, we don�t need to store inside the ImageList a lot of the same icon.
The advantage of this technique is because is too simple. The problem is the TreeView will display only when all the TreeView is fully loaded. The user normally wants something instantly. So to ideas are possible:
- -Use the MouseHover event to load a specific directory
- -Use a Thread to load in background.
If I have to choose, I would use the first idea because is more stable, simple and will give better results.
N.B. Because if an simply using the text as Key in the node items I have to be cautious not accidently using 2 times the same Key String.
Download Sample: SampleBasicProgram
Previous Post : TreeView with recursivity
Previous Post : Create searchBox using UserControl
About
I invite you to visit my blog for more articles and leave a comment. Check Technologies represents more than 10 years .... Computer and computer aided design.Official Website Check Technologies
235 Adrien-Provencher
Beloeil, (Qu�bec), J3G 0C8, Canada
Tel : 514-705-7690
ema: info@checktechno.ca
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :