Windows Develop Bookmark and Share   
 index > Windows Forms General > how to make a treeview show an image for some nodes but not others?
 

how to make a treeview show an image for some nodes but not others?

Hi,

I created a user control that inherits System.Windows.Forms.TreeView so I could use the StateImageList to implementa tri-state checkbox treeview. For some of my nodes I don't have checkboxes, and I found setting node.StateImageIndex = -1 makes the node not display any space for the state image. So that works great.

However, I also want to display images on some nodes and not others. I expected to do the same thing: set node.ImageIndex = -1 for the nodes that I don't want to have an image. But when I do that, the nodes actually show the first image in the ImageList, as if I set the ImageIndex = 0.

So,is there any wayI canshow an image onsome nodes, and not on others?

Thanks,

David

David Burson  Friday, April 27, 2007 4:25 PM
The only way to do this is to draw the nodes yourself with the DrawNode event and the DrawMode property set to OwnerDrawAll. That's very tough to do, you'll also have to draw the lines and the +/- nodes. And you have to override hit testing. I have never seen anybody do this.

One unglamorous trick is to include an empty icon in your image list and select it for nodes you want to show without image. The gap is a bit noticeable of course but at least the node text will line up properly.
nobugz  Friday, May 04, 2007 7:48 PM

Hi

Would mind paste some code here so I can know you problem clearly!

Cael Z  Friday, May 04, 2007 12:11 PM

Sure - here's the idea:

MyTree.ImageList = MyImageList

node1 = new TreeNode("Has Image")

MyTree.Nodes.Add(node1)

node1.ImageListIndex = 0

node2 = new TreeNode("No Image")

MyTree.Nodes.Add(node2)

node2.ImageListIndex = -1

Unfortunately, node2 still displays the first image in the imagelist, as if I set it's ImageListIndex = 0.

What I want is no image to be displayed for node2.

Any thoughts?

Thanks,

David

David Burson  Friday, May 04, 2007 1:25 PM
The only way to do this is to draw the nodes yourself with the DrawNode event and the DrawMode property set to OwnerDrawAll. That's very tough to do, you'll also have to draw the lines and the +/- nodes. And you have to override hit testing. I have never seen anybody do this.

One unglamorous trick is to include an empty icon in your image list and select it for nodes you want to show without image. The gap is a bit noticeable of course but at least the node text will line up properly.
nobugz  Friday, May 04, 2007 7:48 PM
Hi David,

Can you tell me, how to prepare a image List...(in WPF)

I did this::

List<Image> myimgList = new List<Image>;

Image img = new Image();

myimg List.Add(img);


Now how to Give The image to---> ' img '....???

I tried lik this-------- Image img = new Image("~Images/folder.png");(I have images in my project with folder name ' Images ')

But i got anerror---"--Error1'System.Windows.Controls.Image' does not contain a constructor that takes '1' arguments"---

and also tell me how to bind that Image List to TreeView

I prepared a List of menuitems for my treeview and it worked. When i am trying to do it in the same way for images its not working..

Please suggest me the Correct way to do this..

Thank you foryour support.

Venu

Beginner in WPF.......VENU
Venu Volla  Friday, May 22, 2009 6:07 AM

You can use google to search for other answers

Custom Search

More Threads

• [C#]How to change an image's brightness and contrast through GDI+?
• Tab control
• How to play the audio files in Windows Form
• How will I center a text box and a button on a form at runtime?
• Custom ToolBarItem problems
• Customizing AxWebBrowser content while printing
• Screen Refresh taking Forever
• ComboBox bug?
• SelectedIndexChanged - possible bug
• Difference between normal(windows or console) application vs windows service application