Hi all,
Using VS 2005, and I have a problem with removing nodes with custom images
from a treeview.
TreeView's ImageList is initially empty. First, I add a default/root image:
treeView.ImageList.Images.Add("Tree_Root", new Bitmap(Resources.Tree_Root));
treeView.ImageKey = "Tree_Root";
treeView.SelectedImageKey= "Tree_Root";
When adding nodes, I create a custom bitmap (each node gets a different icon),
add it to the image list and assign node's ImageKey and SelectedImageKey:
Bitmap nodeBmp = new Bitmap(...);
// ... draw on bitmap ...
string imageKey = ... // create a unique image key
treeView.ImageList.Images.Add(imageKey, nodeBmp);
newNode.ImageKey = imageKey;
newNode.SelectedImageKey= imageKey;
...
// add the new node
treeView.Nodes[0].Nodes.Add(newNode);
This works fine. The problem occurs when removing one of these nodes that is
not the last:
TreeNode node = // get the node to be removed...
// remove the node from the tree
node.Remove();
// remove node image from the ImageList
treeView.ImageList.Images.RemoveByKey(node.ImageKey);
The problem is that after removing a node, nodes that were below it get their
images screwed up: unselected image for a node is ok, but selected image is
painted as next node's image (the last node's selected image is painted
blank). I have checked their ImageKey and SelectedImageKey properties, and
they're the same, as expected.
If I don't remove node icons from the ImageList, things work ok.
Any ideas?
Thank you,

Signature
Marko
ICQ: 5990814
I'm not under the alkafluence of inkahol
that some thinkle peep I am.
It's just the drunker I sit here the longer I get.
Kevin Spencer - 23 Feb 2007 17:11 GMT
Hi Marko,
The only clue I could find regarding your dilemma is that when you set the
ImageKey property of a TreeNode, it nullifies the ImageIndex, setting it
to -1, and if you set the ImageIndex of a TreeNode, it nullifies the
ImageKey property, setting it to "". In other words, the 2 properties are
mutually exclusive. So, perhaps you are mixing them up somewhere?

Signature
HTH,
Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com
I had the same problem once. Fixed it using the same solution.
> Hi all,
>
[quoted text clipped - 49 lines]
>
> Thank you,
Marko Bozikovic - 26 Feb 2007 08:22 GMT
> Hi Marko,
>
[quoted text clipped - 3 lines]
> ImageKey property, setting it to "". In other words, the 2 properties are
> mutually exclusive. So, perhaps you are mixing them up somewhere?
Nope, I checked that already :)

Signature
Marko
ICQ: 5990814
I'm not under the alkafluence of inkahol
that some thinkle peep I am.
It's just the drunker I sit here the longer I get.