TreeView node image index changes when selected

When I tried using imagelist in treeview, the index of the image changes when treenode is clicked. I have no idea why this is happening. Can anybody help me?

Thanks in advance

+6
c # treeview imagelist
source share
3 answers

You need to install both ImageIndex and SelectedImageIndex in the node tree.

+11
source share

The 'SelectedImageIndex intent allows you to display a different image when selected than what is set to β€œImageIndex" for a specific node. To save these two alignments, you must set them to the same value. This can be done at design time or programmatically depending on your needs.

For example, if the images never change, it is as simple as installing them at the same time when a new node is added to the TreeView:

int myCurrentImageIndex = 0; TreeNode node = myTreeView.Nodes.Add("new node!"); node.ImageIndex = node.SelectedImageIndex = myCurrentImageIndex; 

However, if you change the value of ImageIndex for any reason after its initial creation (for example, a response to some user action), you must also change the value of SelectedImageIndex. Otherwise, they will become inconsistent.

 int myNewImageIndex = 1; node.ImageIndex = node.SelectedImageIndex = myNewImageIndex; 

(Note that it’s not enough to specify that they are the same in the AfterSelect event handler. This should be done anywhere in your code where ImageIndex changes.)

+9
source share

you can directly do this in the constructor:

 TreeNode node = new TreeNode("My treenode", 1, 1); 
+1
source share

All Articles