Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#VB.NET
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.
 
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 23-Dec-12 6:40am
Comments
jibesh at 23-Dec-12 14:35pm
   
in which method/event you are executing the above codes?

1 solution

Rate this: bad
good
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
ImageList1.Images.Add(imgNeutral)
 
and change
n.SelectedImageIndex = nn
to
n.ImageIndex = nn
n.SelectedImageIndex = nn
 
Regards,
Daniele.
  Permalink  
Comments
ExcelledProducts at 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
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,518
1 OriginalGriff 478
2 Manas Bhardwaj 394
3 Tadit Dash 310
4 Damith Weerasinghe 260
0 Sergey Alexandrovich Kryukov 9,575
1 OriginalGriff 5,856
2 Peter Leow 4,405
3 Maciej Los 3,540
4 Abhinav S 3,513


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 27 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid