Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2007

Tip: Looking for answers? Try searching our database.

Removing TreeView nodes with custom images

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marko Bozikovic - 22 Feb 2007 16:54 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.