TreeView中的不同图像 [英] Different Images In TreeView

查看:78
本文介绍了TreeView中的不同图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个程序将项目添加到带有图片的树视图中。问题是,当添加更多项目时,它们都会获得相同的图片。



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





当我开始吧这是我得到的。这是我没有点击第二项时的结果。



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



当我点击第二个节点时。这就是我得到的。

http://i.imgur.com/RewXy.png [ ^ ]



我不想在不同的节点上显示不同的图像而不必点击它。有什么建议吗?



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?

推荐答案

1)你只设置了SelectedImageIndex,忘记了ImageIndex,所以只有当项目被选中时才会出现项目自己的图像。 />
2)没有明确设置图像的所有项目将显示图像列表的第一个表格,因此您应该在ImageList1的顶部添加中性图像。

3)如果ImageList1或者TreeView1要重复使用或多次显示相同的项目,我建议您使用图像键(ImageKey,SelectedImageKey)而不是索引来查看ImageList1是否存在已经显示的图像因此避免再次添加它。



所以...在给树之前你应该调用这样的代码一次:

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


to

n.ImageIndex = nn
n.SelectedImageIndex = nn





问候,

Daniele。



Regards,
Daniele.


这篇关于TreeView中的不同图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆