【问题标题】:Add images to ImageList at runtime in Visual Basic在 Visual Basic 运行时将图像添加到 ImageList
【发布时间】:2014-01-13 15:42:05
【问题描述】:

我正在使用 Visual Studio 2010 并在 VB 中进行编码。

我正在获取文件夹中的目录以填充 ListView,并且这样做没有问题。 我还可以通过在文件夹中搜索文件来将图像添加到 ImageList。 我不能做的就是将两者联系起来。如果我硬编码我的项目并硬编码我的图像,它们就可以正常工作。

我还需要弄清楚如何为每个项目添加标签。标签应该是目录的名称。 (来自第一个 For Each 的目录)

Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    'Set ListView view mode to show Large Icons
    ListView1.View = View.LargeIcon

    Dim dirs As String() = Directory.GetDirectories(filePath & "Resources\IETMS\")
    Dim dir As String

    For Each dir In dirs
        dir = dir.Replace(dir.Substring(0, dir.LastIndexOf("\") + 1), "")
        ListView1.Items.Add(New ListViewItem(dir))
    Next

    'Create ImageList objects. 
    Dim imageListLarge As New ImageList()
    imageListLarge.ImageSize = New Size(100, 100)

    Dim filesList As String() = Directory.GetFiles(filePath & "Resources\IETMS\")
    Dim files As String
    For Each files In filesList
        'files = files.Replace(files.Substring(0, files.LastIndexOf("\") + 1), "")
        imageListLarge.Images.Add(Bitmap.FromFile(files))
    Next

    'Assign the ImageList objects to the ListView.
    ListView1.LargeImageList = imageListLarge

End Sub

这是相同的代码,但对信息进行了硬编码。 下一个示例完全按照我想要的方式工作。 唯一的问题是有些客户可能拥有全部 6 件物品,而有些客户可能没有。 这就是需要 For Each 的原因。

Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'Set ListView view mode to show Large Icons
        ListView1.View = View.LargeIcon

        ' Create items for the listview
        Dim item1 As New ListViewItem("Item 1", 5)
        Dim item2 As New ListViewItem("Item 2", 4)
        Dim item3 As New ListViewItem("Item 3", 0)
        Dim item4 As New ListViewItem("Item 4", 3)
        Dim item5 As New ListViewItem("Item 5", 1)
        Dim item6 As New ListViewItem("Item 6", 2)

        'Set the group for the items
        item1.Group = ListView1.Groups("IETMs")
        item2.Group = ListView1.Groups("IETMs")
        item3.Group = ListView1.Groups("IETMs")
        item4.Group = ListView1.Groups("IETMs")
        item5.Group = ListView1.Groups("IETMs")
        item6.Group = ListView1.Groups("IETMs")

        'Set a tag for the items
        item1.Tag = "This is file 1"
        item2.Tag = "This is file 2"
        item3.Tag = "This is file 3"
        item4.Tag = "This is file 4"
        item5.Tag = "This is file 5"
        item6.Tag = "This is file 6"

        'Add the items to the ListView.
        ListView1.Items.AddRange(New ListViewItem() {item1, item2, item3, item4, item5, item6})

        'Create ImageList objects. 
        Dim imageListLarge As New ImageList()
        imageListLarge.ImageSize = New Size(100, 100)

        ' Initialize the ImageList objects with bitmaps
        Dim image1 = Bitmap.FromFile(filePath & "Resources\IETMS\file1.ico")
        Dim image2 = Bitmap.FromFile(filePath & "Resources\IETMS\file2.ico")
        Dim image3 = Bitmap.FromFile(filePath & "Resources\IETMS\file3.ico")
        Dim image4 = Bitmap.FromFile(filePath & "Resources\IETMS\file4.ico")
        Dim image5 = Bitmap.FromFile(filePath & "Resources\IETMS\file5.ico")
        Dim image6 = Bitmap.FromFile(filePath & "Resources\IETMS\file6.ico")

        imageListLarge.Images.AddRange({image1, image2, image3, image4, image5, image6})

        'Assign the ImageList objects to the ListView.
        ListView1.LargeImageList = imageListLarge

    End Sub

这是我修改后的代码。

Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'Set ListView view mode to show Large Icons
        ListView1.View = View.LargeIcon

        Dim dirs As String() = Directory.GetDirectories(filePath & "Resources\IETMS\")
        Dim dir As String

        For Each dir In dirs
            dir = dir.Replace(dir.Substring(0, dir.LastIndexOf("\") + 1), "")
            ListView1.Items.Add(New ListViewItem(dir, dir))
        Next

        'Create ImageList objects. 
        Dim imageListLarge As New ImageList()
        imageListLarge.ImageSize = New Size(100, 100)

        Dim filesList As String() = Directory.GetFiles(filePath & "Resources\IETMS\")
        Dim files As String
        Dim files2 As String

        For Each files In filesList
            files2 = files.Replace(files.Substring(0, files.LastIndexOf("\") + 1), "")
            files2 = files2.Replace(".ico", "")
            imageListLarge.Images.Add(files2, Bitmap.FromFile(files))
        Next

        'Assign the ImageList objects to the ListView.
        ListView1.LargeImageList = imageListLarge

    End Sub

【问题讨论】:

    标签: vb.net listview imagelist


    【解决方案1】:

    ImageList集合项也可以带键方便查找:

    imageListLarge.Images.Add(files, Bitmap.FromFile(files)))
    

    那你就可以按键引用了:

    Dim bmp As Bitmap = imageListLarge.Images("my file name")
    

    添加 ListView 项时,可以使用 ImageKey 作为参考:

    Dim item1 As New ListViewItem("2J-F108-100", "2J-F108-100")
    

    这假设您使用相同的键添加图像:

    imageListLarge.Images.Add("2J-F108-100", Bitmap.FromFile(...)))
    

    另外,请确保在添加 ListItems 之前添加您的图片:

    Dim imageListLarge As New ImageList()
    imageListLarge.ImageSize = New Size(100, 100)
    Dim filesList As String() = Directory.GetFiles(filePath & "Resources\IETMS\")
    Dim files2 As String
    For Each files As String In filesList
      files2 = files.Replace(files.Substring(0, files.LastIndexOf("\") + 1), "")
      files2 = files2.Replace(".ico", "")
      imageListLarge.Images.Add(files2, Bitmap.FromFile(files))
    Next
    ListView1.LargeImageList = imageListLarge
    
    ListView1.View = View.LargeIcon
    Dim dirs As String() = Directory.GetDirectories(filePath & "Resources\IETMS\")
    For Each dir As String In dirs
      dir = dir.Replace(dir.Substring(0, dir.LastIndexOf("\") + 1), "")
      ListView1.Items.Add(New ListViewItem(dir, dir))
    Next
    

    【讨论】:

    • 不太确定这对我有什么帮助。如果你能解释这对我的情况有什么作用,我将不胜感激。我非常感谢您的意见。
    • @xRuhRohx 更新了帖子。您不想使用索引值来链接图像,而是想使用键。使用文件名还是您自己的密钥取决于您。
    • 好的,我想我明白了。我填充了 ListView 并填充了 imageList。但是图像仍然没有显示给我。无论如何,看看它们在运行时是否匹配?就像循环遍历两个列表并查看它们是否匹配?
    • 我遍历 ListView,它显示了所有项目,ImageList 显示了 6 个项目,这是正确数量的项目,但我的图像没有显示。
    • @xRuhRohx 好吧,我看不出你做了什么。您对图像使用什么“键”以及对 ListViewItem 的 ImageKey 属性使用什么键?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多