Treeview Icons Problem

curlydog

Well-known member
Joined
Jul 5, 2010
Messages
50
Programming Experience
Beginner
Hi,
I have a treeview where I want to assign icons to only certain nodes. For some nodes I do not want an icon at all. I'm using an imageList to specify the icons and when a specific kind of node is added the relevant icon is assigned (using imageKey) from the imageList.

The problem I am having is that where I do not assign an icon to a node, one is displayed anyway. It appears that if i do not assign an icon, the node displays the image from position 0 in my imageList.

I have tried setting the imageKey value to -1 for the nodes that shouldn't have an icon, but I still get the icon from position 0 displayed.

Is there a way around this, or am I missing something simple.

thanks
Jason
 
I'm not sure that it will work but it would be the ImageIndex you set to -1, not the ImageKey. If that doesn't work, create an "empty" Image and make that the first item in the ImageList.
 
Thanks for the reply. You're right, about using ImageIndex rather than ImageKey. I've tried both and settled with ImageIndex and got the results as described in my original post.

I also tried using an empty image as the first item in my ImageList. I used a clearpixel (single coloured GIF withe the single colour set to transparent). Although it was better, I ended up with a blank square which hid the dotted connecting lines in the TreeView. I may be a bit picky, but it wasn't aesthetically pleasing. I may have to settle with this compromise, unless someone else can suggest a better way.

Thanks
Jason
 
OK, it appears that there is a bug in the TreeView control. A value of -2 for the ImageIndex property is supposed to indicate no Image. If you edit the nodes and select (none) for the ImageIndex then the Nodes editor does show no Image, but the form designer shows the first Image. I just ran this code:
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For Each node As TreeNode In Me.TreeView1.Nodes
        If node.ImageIndex = -2 Then
            node.ImageIndex = -2
        End If

        If node.SelectedImageIndex = -2 Then
            node.SelectedImageIndex = -2
        End If
    Next
End Sub
after selecting (none) in the designer and it worked as you want.
 
I've tried your suuggestion, but still with the same result. The nodes on my TreeView are created programatically, so I can't edit their properties in the designer. I did however set the ImageIndex of the TreeView to none.

I've tried setting the image at the time that the nodes are created and added. Now I'm trying to recurse through the nodes (I have several levels) after all of the nodes are added.

Here's the code I'm using;

VB.NET:
'Sets the icons within the treeviw, depending on the object type
    Private Sub setTreeviewIcons()
        Debug.Print("frmSearch.setTreeViewIcons: INTO ROUTINE")
        For Each aNode As TreeNode In Me.TreeView1.Nodes
            recurseNodes(aNode)
        Next
        Debug.Print("frmSearch.setTreeViewIcons: END ROUTINE")
    End Sub

    Private Sub recurseNodes(ByVal a As TreeNode)
        If InStr(a.Name, "Owner") > 0 Then
            Debug.Print("frmSearch.setTreeViewIcons: Node is Owner")
            a.ImageIndex = 1
            a.SelectedImageIndex = 2
        ElseIf InStr(a.Name, "Handset") > 0 Then
            Debug.Print("frmSearch.setTreeViewIcons: Node is Handset")
            a.ImageIndex = 3
            a.SelectedImageIndex = 4
        ElseIf InStr(a.Name, "SIM") > 0 Then
            Debug.Print("frmSearch.setTreeViewIcons: Node is SIM")
            a.ImageIndex = 5
            a.SelectedImageIndex = 6
        Else
            Debug.Print("frmSearch.setTreeViewIcons: Node not determined")
            a.ImageIndex = -2
            a.SelectedImageIndex = -2
        End If

        If a.ImageIndex = -2 Then
            a.ImageIndex = -2
        End If

        If a.SelectedImageIndex = -2 Then
            a.SelectedImageIndex = -2
        End If

        Dim aNode As TreeNode
        For Each aNode In a.Nodes
            recurseNodes(aNode)
        Next
    End Sub

It's working fine for the nodes where I specify an image from the ImageList. Where the ImageIndex is set to -2, I still get the picture from index 0 of the ImageList.

Thanks
Jason
 
You can in code set index to -2 or one higher than image count in imagelist (index = SomeImageList.Images.Count).
Value -2 can not be set in the Nodes.Add("blah", index) method or with the TreeNode constructor, but have to be set after node is added to the TreeView to take effect.
 
You can in code set index to -2 or one higher than image count in imagelist (index = SomeImageList.Images.Count).
Value -2 can not be set in the Nodes.Add("blah", index) method or with the TreeNode constructor, but have to be set after node is added to the TreeView to take effect.

I think that is what I'm doing.
In adding the nodes to the Treeview in one step. When that is finished, I use the above code to determine the type of node and then assign an ImageIndex to it. Where I don't want an image, I assign an ImageIndex of -2.

Essentially I'm now using a two setp process. First to populate the TreeView. Second to assign images.
 
Back
Top