VB.net用文件夹中的图像填充Listview

时间:2019-05-29 01:35:16

标签: vb.net

我有一些ini文件,其中包含ini文件的图像名称。 我有这个功能:

Public Function GetAllSongs()
    Try
        While Me.ListView3.Items.Count > 0
            Me.ListView3.Items.Remove(Me.ListView3.Items(0))
            Me.ListView3.Columns.Remove(Me.ListView3.Columns(0))
        End While
    Catch
    End Try

    If Directory.GetFiles(My.Computer.FileSystem.CurrentDirectory & "\Files\Songs\").Length = 0 Then
        ListView3.Columns.Add("Cover", 53, HorizontalAlignment.Left)
        ListView3.Columns.Add("Title", 145, HorizontalAlignment.Left) '163
        ListView3.Columns.Add("Views", 245, HorizontalAlignment.Left) '163
        ListView3.AllowColumnReorder = True
        ListView3.Columns(1).DisplayIndex = 1

        Dim index As Integer = 0
        Dim lvi As New ListViewItem
        lvi.ImageIndex = index
        lvi.SubItems.Add("Not songs")
        lvi.SubItems.Add("Not songs")
        ListView3.Items.Add(lvi)
    Else

        ListView3.Columns.Add("Cover", 100, HorizontalAlignment.Left)
        ListView3.Columns.Add("Title", 145, HorizontalAlignment.Left) '163
        ListView3.Columns.Add("Views", 145, HorizontalAlignment.Left) '163
        Dim di As New DirectoryInfo(My.Computer.FileSystem.CurrentDirectory & "\Files\Songs\")
        Dim fiArr As FileInfo() = di.GetFiles()
        Dim fri As FileInfo
        For Each fri In fiArr


            Dim Covername As String = My.Computer.FileSystem.CurrentDirectory & "\Files\Covers\" & Inireader.WertLesen("Details", "ID", My.Computer.FileSystem.CurrentDirectory & "\Files\Songs\" & fri.Name)
            Dim Trackname As String = Inireader.WertLesen("Details", "TrackName", My.Computer.FileSystem.CurrentDirectory & "\Files\Songs\" & fri.Name)
            Dim Views As String = Inireader.WertLesen("Details", "Views", My.Computer.FileSystem.CurrentDirectory & "\Files\Songs\" & fri.Name)

            Dim imgList As New ImageList



            ListView3.AllowColumnReorder = True
            ListView3.Columns(1).DisplayIndex = 1
            imgList.ImageSize = New Size(70, 70)
            imgList.Images.Add("Cover", Image.FromFile(Covername & ".jpg"))
            ListView3.SmallImageList = imgList


            Dim index As Integer = 0
            Dim lvi As New ListViewItem
            lvi.ImageIndex = index
            lvi.SubItems.Add(Trackname)
            lvi.SubItems.Add(Views)
            ListView3.Items.Add(lvi)


        Next fri
    End If
End Function

问题:它只显示一个封面,而不是每个文件都显示不同的封面。

我的ini文件结构:

 [Details] ID=6W6H3BCUZ5s5wZlKWdICOc
 URL=https://open.spotify.com/track/6W6H3BCUZ5s5wZlKWdICOc?si=5ehoney8QoC-q9FGqeY_ew
 TrackName=Shortcut

现在看起来像这样:

enter image description here

我认为这与索引有关。但是,例如,当我将其从0更改为1时,就没有封面了。

您有什么想法吗?

谢谢:) 最好的问候

1 个答案:

答案 0 :(得分:1)

在for循环外声明ImageList,并在每次迭代后增加计数值。

Try
    While Me.ListView3.Items.Count > 0
        Me.ListView3.Items.Remove(Me.ListView3.Items(0))
        Me.ListView3.Columns.Remove(Me.ListView3.Columns(0))
    End While
Catch
End Try

If Directory.GetFiles(My.Computer.FileSystem.CurrentDirectory & "\Files\Songs\").Length = 0 Then
    ListView3.Columns.Add("Cover", 53, HorizontalAlignment.Left)
    ListView3.Columns.Add("Title", 145, HorizontalAlignment.Left) '163
    ListView3.Columns.Add("Views", 245, HorizontalAlignment.Left) '163
    ListView3.AllowColumnReorder = True
    ListView3.Columns(1).DisplayIndex = 1

    Dim index As Integer = 0
    Dim lvi As New ListViewItem
    lvi.ImageIndex = index
    lvi.SubItems.Add("Not songs")
    lvi.SubItems.Add("Not songs")
    ListView3.Items.Add(lvi)
Else

    ListView3.Columns.Add("Cover", 100, HorizontalAlignment.Left)
    ListView3.Columns.Add("Title", 145, HorizontalAlignment.Left) '163
    ListView3.Columns.Add("Views", 145, HorizontalAlignment.Left) '163
    Dim di As New DirectoryInfo(My.Computer.FileSystem.CurrentDirectory & "\Files\Songs\")
    Dim fiArr As FileInfo() = di.GetFiles()
    Dim fri As FileInfo

    Dim imgList As New ImageList

    ListView3.AllowColumnReorder = True  
    ListView3.Columns(1).DisplayIndex = 1
    imgList.ImageSize = New Size(70, 70)
    ListView3.SmallImageList = imgList
    Dim index As Integer = 0

    For Each fri In fiArr
        Dim Covername As String = My.Computer.FileSystem.CurrentDirectory & "\Files\Covers\" & Inireader.WertLesen("Details", "ID", My.Computer.FileSystem.CurrentDirectory & "\Files\Songs\" & fri.Name)
        Dim Trackname As String = Inireader.WertLesen("Details", "TrackName", My.Computer.FileSystem.CurrentDirectory & "\Files\Songs\" & fri.Name)
        Dim Views As String = Inireader.WertLesen("Details", "Views", My.Computer.FileSystem.CurrentDirectory & "\Files\Songs\" & fri.Name)

           imgList.Images.Add("Cover", Image.FromFile(Covername & ".jpg"))

        Dim lvi As New ListViewItem
        lvi.ImageIndex = index

        'Increase the index
        index += 1

        lvi.SubItems.Add(Trackname)
        lvi.SubItems.Add(Views)
        ListView3.Items.Add(lvi)
    Next fri
End If