Vb.net 在ListView对象中显示应用程序图标(和名称)

Vb.net 在ListView对象中显示应用程序图标(和名称),vb.net,icons,ms-office,Vb.net,Icons,Ms Office,我, 我正在使用ListView对象创建一个VB表单,该对象包含Excel、Word或Pdf图标的图像,并带有指向所显示文档的链接(待制作) 编译时,文档的名称显示在listView中,但不显示图标。你知道我的代码缺少什么吗 据我所知,“ExtractAssociatedIcon”方法需要文件的完整路径,但这里似乎没有收集图标 谢谢 Imports System Imports System.IO Imports System.Drawing [……] 1)您需要设置列表视图的和/或属性: L

我,

我正在使用ListView对象创建一个VB表单,该对象包含Excel、Word或Pdf图标的图像,并带有指向所显示文档的链接(待制作)

编译时,文档的名称显示在listView中,但不显示图标。你知道我的代码缺少什么吗

据我所知,“ExtractAssociatedIcon”方法需要文件的完整路径,但这里似乎没有收集图标

谢谢

Imports System
Imports System.IO
Imports System.Drawing
[……]

1)您需要设置
列表视图的和/或属性:

ListView1.LargeImageList = ImageList1
ListView1.SmallImageList = ImageList1
2)将其放在代码的顶部。(不在每个循环的
中)

3)此外,请确保不要添加空图标,也不要设置无效的图像键:

If (ImageList1.Images.ContainsKey(exePath)) Then
    ListView1.Items.Add(fileInfo.Name, exePath)
ElseIf (Not exeIcon Is Nothing) Then
    ImageList1.Images.Add(exePath, exeIcon)
    ListView1.Items.Add(fileInfo.Name, exePath)
Else
    ListView1.Items.Add(fileInfo.Name)
End If
示例

以下代码经过测试,工作正常:

ListView1.LargeImageList = ImageList1
ListView1.SmallImageList = ImageList1
ListView1.View = View.LargeIcon

Dim dirInfo As DirectoryInfo
Dim fileInfo As FileInfo
Dim exeIcon As System.Drawing.Icon

dirInfo = New DirectoryInfo("...")

For Each fileInfo In dirInfo.GetFiles
    If (Not String.IsNullOrEmpty(fileInfo.Extension)) Then

        exeIcon = System.Drawing.Icon.ExtractAssociatedIcon(fileInfo.FullName)

        If (ImageList1.Images.ContainsKey(fileInfo.FullName)) Then
            ListView1.Items.Add(fileInfo.Name, fileInfo.FullName)
        ElseIf (Not exeIcon Is Nothing) Then
            ImageList1.Images.Add(fileInfo.FullName, exeIcon)
            ListView1.Items.Add(fileInfo.Name, fileInfo.FullName)
        Else
            ListView1.Items.Add(fileInfo.Name)
        End If

    End If
Next
If (ImageList1.Images.ContainsKey(exePath)) Then
    ListView1.Items.Add(fileInfo.Name, exePath)
ElseIf (Not exeIcon Is Nothing) Then
    ImageList1.Images.Add(exePath, exeIcon)
    ListView1.Items.Add(fileInfo.Name, exePath)
Else
    ListView1.Items.Add(fileInfo.Name)
End If
ListView1.LargeImageList = ImageList1
ListView1.SmallImageList = ImageList1
ListView1.View = View.LargeIcon

Dim dirInfo As DirectoryInfo
Dim fileInfo As FileInfo
Dim exeIcon As System.Drawing.Icon

dirInfo = New DirectoryInfo("...")

For Each fileInfo In dirInfo.GetFiles
    If (Not String.IsNullOrEmpty(fileInfo.Extension)) Then

        exeIcon = System.Drawing.Icon.ExtractAssociatedIcon(fileInfo.FullName)

        If (ImageList1.Images.ContainsKey(fileInfo.FullName)) Then
            ListView1.Items.Add(fileInfo.Name, fileInfo.FullName)
        ElseIf (Not exeIcon Is Nothing) Then
            ImageList1.Images.Add(fileInfo.FullName, exeIcon)
            ListView1.Items.Add(fileInfo.Name, fileInfo.FullName)
        Else
            ListView1.Items.Add(fileInfo.Name)
        End If

    End If
Next