last day (16 days later) » 

11:36 PM
0
Q: TreeView node icon changes when selected only on custom icons in C#

DavidSo I have a TreeView in C# and I'm loading icons for folders via P/Invoke. It's working, but on icons which are custom (so to speak), they change to some weird icon, as seen in the image below: On "Regular" folders, i.e. ones without custom icons, this doesn't happen. Also on things like my HD...

 
TaW
Is this Winforms? Is node a TreeNode? Where and how is UniqueIcon defined?
 
Yes. UniqueIcon is a combination of SHFILEINFO's dwAttributes and iIcon properties, joined into a string. The uniqueness is working from what I can tell so far. For the icon in question, it's "0 49".
 
TaW
Hm. Are those icons actually sitting in an ImageList?
 
Yes. SmallImageList.Images.Add(FileInfo.UniqueIcon, Icon.FromHandle(FileInfo.Icon));
 
TaW
Ah, ok. I assume that the whole problem is reproducible? Can you log out the FileInfo.UniqueIcon string in a Console.WriteLine? I bet that they are not unique. Now this is a strange, lets call it quirk, in ImageList.Add: The keys need not be unique and therefore if you try to access the images by their string key you will only get the ones where the key was inserted first..
Or you could print out all keys in the ImageList. - Btw: a Treeview in Winforms doesn't have a SmallImageList only a ImageList and a StateImageList. SmallImageList s are used in ListViews
 
11:36 PM
Sorry, wrong method. The other one is identical except for that one bit. This is straight from the dubugger: imgur.com/S4AdARh
 
TaW
OK, these are identical keys. I guess they are the nodes? But are they unique in the ImageList? Consider: This doesn't throw: ImageList IL = new ImageList(); Bitmap bmp = new Bitmap(66, 66); IL.Images.Add("1"); IL.Images.Add("2"); IL.Images.Add("1", bmp); so the concept of 'Key' is somewhat weak here, imo.. Maybe for duplicate keys we get a random item of those with that key? It is reproducible?
 
The top part of that image is what happens when I enumerate the keys in the image list. The bottom part is showing that the node does have identical keys for both key indexes.
Oh, and I also ensure that I never add a duplicate key.
public void AddIcon(FileDirInfo FileInfo) {
if (!ImageList.Images.ContainsKey(FileInfo.UniqueIcon)) {
ImageList.Images.Add(FileInfo.UniqueIcon, Icon.FromHandle(FileInfo.Icon));
}
}
 

  last day (16 days later) »