Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
So i have this program that adds items to a tree view with a picture. The problem is that when a add more items they all get the same picture.

VB
Try
            For Each itm In m
                If itm.StartsWith("MS") Then
                    Dim parts() As String
                    parts = itm.Split(":")

                    Dim lnk = parts(1) & ":" & parts(2)
                    Dim img_type = parts(4)

                    WC.DownloadFile(New Uri(lnk), tmp & nn & parts(4).ToString)

                    Dim img As Image
                    img = Image.FromFile(tmp & nn & parts(4).ToString)

                    ImageList1.Images.Add(img)

                    Dim nfont As New Font("Helvetica", 9, System.Drawing.FontStyle.Regular)

                    TreeView1.SelectedImageIndex = Nothing
                    Dim n As New TreeNode
                    n.Name = nn.ToString
                    n.Text = parts(3)
                    n.SelectedImageIndex = nn


                    TreeView1.Nodes.Add(n)

                    'My.Computer.FileSystem.DeleteFile(tmp & nn & parts(4).ToString)

                    nn = nn + 1
                End If
            Next

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


When i start it out this is what I get. This is what i get when i don't click on the second item.

http://i.imgur.com/80s79.png[^]

When i click on the second node. This is what i get.
http://i.imgur.com/RewXy.png[^]

I wan't it to display a different image on different nodes without having to click on it. Any suggestions?
Posted
Comments
Jibesh 23-Dec-12 14:35pm    
in which method/event you are executing the above codes?

1 solution

1) You are setting only SelectedImageIndex, forgetting about ImageIndex, so the item's own image appears only when the item gets selected.
2) All the items without an image explicitly set will display the first form the image list, so you should add a neutral image on top of ImageList1.
3) If the ImageList1 or the TreeView1 are meant to be reused or display the same items more than once, I suggest you to use image keys (ImageKey, SelectedImageKey) instead of indices to be able to look into the ImageList1 for the existence of an image from an already displayed item thus avoiding adding it again.

so... you should call code like this once, before feeding the tree:
Dim imgNeutral As Image
imgNeutral = Properties.Resources.NeutralImage
ImageList1.Images.Add(imgNeutral)


and change
n.SelectedImageIndex = nn

to
n.ImageIndex = nn
n.SelectedImageIndex = nn


Regards,
Daniele.
 
Share this answer
 
Comments
ExcelledProducts 28-Dec-12 16:13pm    
Works exactly like I want it to thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900