Removing TreeView nodes with custom images

M

Marko Bozikovic

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,
--
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.
 
K

Kevin Spencer

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?

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

I had the same problem once. Fixed it using the same solution.
 
M

Marko Bozikovic

Kevin said:
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?

Nope, I checked that already :)

--
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top