[ACCEPTED]-How do I set an image for some but not all nodes in a TreeView?-treeview

Accepted answer
Score: 12

You need to set ImageIndex and SelectedImageIndex to a number that is 7 higher than the number of values in your 6 ImageList. For example, if you create this node and 5 add it to your TreeView:

TreeNode node1 = new TreeNode(string.Empty, 12, 12); // imageList1.Count = 5

you will have an invisible 4 TreeNode inserted into your TreeView. I changed the background 3 color of my TreeView and it was still invisible.

(I 2 googled this for some time, and I eventually 1 found the answer here: http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.windowsforms/2006-09/msg00322.html)

Score: 11

What I have chosen to do is to use an image 5 of dots for those TreeView nodes that are not supposed 4 to have an image.

TreeNode with image dots

I add this image as the 3 last image in the list, and if the item 2 is not supposed to have an image I set it 1 to ImageList.Images.Count-1

Score: 10

I found that using StateImageList on the 3 TreeView instead of ImageList will only 2 show the image when StateImageIndex on the 1 TreeNode is equal or greater than 0

Score: 8

I tried this once and I don't think it is 7 possible.

If you try to set both ImageKey and ImageIndex to 6 "not set" values the control just defaults 5 ImageIndex to 0. The following code:

treeView.ImageKey = "Value";
treeView.ImageKey = null;
treeView.ImageIndex = -1;

Produces output:


This 4 kind of tells you that the control developers 3 wanted to make sure that there was always 2 a default image. That just leaves you with 1 the hack options I'm afraid.

Score: 3

This will draw the TreeNode text where the image should have been, getting rid of the white space.

You'll need to set the TreeView's DrawMode property to OwnerDrawText. You 13 can find the DrawMode property in the properties 12 panel.

Next when you add a node, set it's 11 ImageIndex and SelectedImageIndex greater than the value of your yourImageListName.Images.Count value. This 10 is so no image will be drawn, but there 9 will still be that white space you don't 8 want.

Now to get rid the white space. Add 7 a handle for the treeviews DrawNode event. This 6 can be done by going to the treeviews property 5 panel and clicking the Icon in the panel 4 that looks like a lighting bolt, then scroll 3 till you see the text DrawNode, double click it.

Now 2 you just copy and paste this into the created 1 method

if (e.Node.ImageIndex >= e.Node.TreeView.ImageList.Images.Count) // if there is no image
    int imagewidths = e.Node.TreeView.ImageList.ImageSize.Width;
    int textheight = TextRenderer.MeasureText(e.Node.Text, e.Node.NodeFont).Height;
    int x = e.Node.Bounds.Left - 3 - imagewidths / 2;
    int y = (e.Bounds.Top + e.Bounds.Bottom) / 2+1;

    Point point = new Point(x - imagewidths/2, y - textheight/2); // the new location for the text to be drawn

    TextRenderer.DrawText(e.Graphics, e.Node.Text, e.Node.NodeFont, point, e.Node.ForeColor);
else // drawn at the default location
    TextRenderer.DrawText(e.Graphics, e.Node.Text, e.Node.TreeView.Font, e.Bounds, e.Node.ForeColor);
Score: 0

Hei bro, i found a way. Set the first image 2 as an empty image, like this...

TreeView treeView = new TreeView();
treeView.ImageList.Images.Add(new Bitmap(1,1));

So, the index 1 0 is an empty image. I hope this helps

More Related questions