Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set an image for some but not all nodes in a TreeView?

Tags:

I have a TreeView windows forms control with an ImageList, and I want some of the nodes to display images, but the others to not have images.

I don't want a blank space where the image should be. I don't want an image that looks like the lines that the TreeView would draw if it didn't have an ImageList. How do I get it to draw images for some items and not others, without resorting to clumsy hacks like that?

like image 606
Simon Avatar asked Nov 04 '08 12:11

Simon


People also ask

How do you edit TreeView?

The TreeView allows you to edit nodes by setting the allowEditing property to true. To directly edit the nodes in place, double click the TreeView node or select the node and press F2 key. When editing is completed by focus out or by pressing the Enter key, the modified node's text saves automatically.


2 Answers

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

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

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

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

like image 141
John Fischer Avatar answered Oct 25 '22 00:10

John Fischer


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

TreeNode with image dots

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

like image 30
MtnManChris Avatar answered Oct 25 '22 02:10

MtnManChris