Click here to Skip to main content
13,057,089 members (44,317 online)
Rate this:
Please Sign up or sign in to 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.

            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)
                    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

                    'My.Computer.FileSystem.DeleteFile(tmp & nn & parts(4).ToString)
                    nn = nn + 1
                End If
        Catch ex As Exception
        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.[^]

When i click on the second node. This is what i get.[^]

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

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

and change
n.SelectedImageIndex = nn

n.ImageIndex = nn
n.SelectedImageIndex = nn

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170728.6 | Last Updated 27 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100