0

我正在使用后台工作人员来填充列表视图,但我也想添加子项。谁能帮我吗?

    Public Class Form1

    Private Sub bgw_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
        Dim li As New List(Of ListViewItem)

        For Each fn As String In My.Computer.FileSystem.GetFiles("s:\Videos", FileIO.SearchOption.SearchAllSubDirectories, "*.*")
            li.Add(New ListViewItem(My.Computer.FileSystem.GetName(fn))) 
            'here i want to add a subitem containing the filesize
            'My.Computer.FileSystem.GetFileInfo(fn).Length
        Next

        e.Result = li.ToArray
    End Sub

    Private Sub bgw_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
        lv.Items.AddRange(DirectCast(e.Result, ListViewItem()))
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        bgw.RunWorkerAsync()
    End Sub
End Class
4

2 回答 2

2

For Each在你的循环中试试这个:

Dim NewItem as New ListViewItem(My.Computer.FileSystem.GetName(fn))
NewItem.SubItems.Add(My.Computer.FileSystem.GetFileInfo(fn).Length)

li.Add(NewItem) 

希望这应该可以解决问题

于 2013-03-14T19:31:05.060 回答
1

这也有效,但它正确吗?

Public Class Form1

    Dim item1 As String = ""
    Dim item2 As String = ""

    Private Sub bgw_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
        Dim progress As Integer = 0
        'calculate progress later
        progress = 1

        For Each fn As String In My.Computer.FileSystem.GetFiles("s:\Videos", FileIO.SearchOption.SearchAllSubDirectories, "*.*")
            item1 = My.Computer.FileSystem.GetName(fn)
            item2 = My.Computer.FileSystem.GetFileInfo(fn).Length

            bgw.ReportProgress(progress)
        Next
    End Sub

    Private Sub bgw_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw.ProgressChanged
        Dim li As New ListViewItem
        li = lv.Items.Add(item1, 0)
        li.SubItems.Add(item2)
    End Sub

    Private Sub bgw_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        lv.Items.Clear()
        bgw.RunWorkerAsync()
    End Sub

End Class
于 2013-03-14T20:09:26.217 回答