how do i add an icon to a treenode?
i don't want to necessarily use an imagelist...
Printable View
how do i add an icon to a treenode?
i don't want to necessarily use an imagelist...
Why would you NOT want to use an imagelist? It may be easier to dynamically add images to the imagelist and then add the treenode using that imageindex.
i have a treeview, which is merely a file/folder structure. when i add a file, i want to associate it with its icon.
so, when i find a file, i add the treenode and then i get the icon associated with the file. if i use an imagelist then i need to see if that icon is already part of the imagelist and find its index... thats tedious.
if you have a thought how i might more effectively use an imagelist tell me, because right now it dosn't make much sense to me for what i'm trying to do.
check this thread out
this should answer your question.
ok, so now i can get the icon of a file or folder. how do i add that icon to a treenode? i have to first convert it to an image file ifi want to use an imageList...Code:Private Declare Function Extract_Icon Lib "shell32.dll" Alias "ExtractIconEx" (ByVal lpszExeFileName As String, ByVal nIconIndex As Integer, ByRef hIconLarge As IntPtr, ByRef hIconSmall As IntPtr, ByVal IconCount As Integer) As IntPtr
Public Function ExtractIconFromFile(ByVal Filename As String, ByVal IconIndex As Integer, ByVal IconSize As iSize) As Drawing.Icon
Dim hIconLarge As IntPtr
Dim hIconSmall As IntPtr
Extract_Icon(Filename, IconIndex, hIconLarge, hIconSmall, 1)
If IconSize = iSize.Large Then
Return Drawing.Icon.FromHandle(hIconLarge)
Else
Return Drawing.Icon.FromHandle(hIconSmall)
End If
End Function
You don't need to convert it to a file to add it to an imagelist. It returns an icon object and that can be added directly to theh imagelist at runtime:
ImageList1.Images.Add(myIcon)
The only hard part would be finding out if it is already in the list. The easiest way around that would probably be to track which extensions you already have images for:
Public ht As New HashTable 'class level scope
ImageList1.Images.Add(myIcon)
ht.Add(extension, myIcon)
'or
ht.Add(extension, myIconIndex)
Then you could retrieve the image or index via the file extension since it is the key for the hashtable.
in the code in my last post, what is iSize.large??? its underlined in my version and not recognized.
try putting this in your "imports" section:
System.Drawing.Icon
look up the icon class and it may have further infor you could use.
also, in that post you got the code from, there is a structure that defines iIcon:
VB Code:
Private Structure SHFileInfo Public hIcon As IntPtr Public iIcon As Integer Public dwAttributes As Integer Public szDisplayName As String Public szTypeName As String End Structure
imports system.drawing.icon dosn't help?
the size class also dosn't have a large property.
what size is probably meant by large? then i can just compare width and height properties...
Hi Marvin.
iSize is just a homemade enum, to determine if the function should return the large or the small icon.
That is why your VB doesn't recognize it.
If you now you always want the small just remove the if statement and use the Return Drawing.Icon.FromHandle(hIconSmall)