Search code examples
c#treeviewimagelist

TreeView and TreeNode, Images from ImageList not always showing


I have a TreeView and I have assigned an ImageList to the the TreeView:

mlstImg.Images.Add(Resources.EMPTY);
mlstImg.Images.Add(Resources.MIXED);
mlstImg.Images.Add(Resources.DOWN);
mlstImg.Images.Add(Resources.UP);
this.TreeViewSites.ImageList = mlstImg;
this.TreeViewSites.ImageKey = null;
this.TreeViewSites.SelectedImageIndex = -1;
this.TreeViewSites.SelectedImageKey = null;

The problem I am seeing is that sometimes an image is not displayed for a node, yet I know each and every node is assigned an ImageIndex.

Why would sometimes an image not be displayed?

IDE: Microsoft Visual Studio Professional 2022 (64-bit), Version 17.8.6
OS: Microsoft Windows [10.0.19045.4291]

[Edit] It seems if the node is expanded then setting the ImageIndex has no effect.

[Edit2] More specifically, its if a node is selected then the ImageIndex has no effect.


Solution

  • Found this: Image index of TreeView node changes upon selection

    I have to use the SelectedImageIndex