Visual Basic 2008 BackgroundWorker Listview

disrupt
disrupt used Ask the Experts™
on
I want to take the following code and instead of a Listbox (CSVListBox) I want to use a Listview (CSVListView) How can I convert the code to use a listview? I tried substituting but I got an error.  Assuming my columns are already done, I have two columns one for File and the other for path so when i hit the start button it should add the file which it already does but also the file path on the next column.
Public Class Form1

    Private WithEvents BGW As New System.ComponentModel.BackgroundWorker

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not BGW.IsBusy Then
            BGW.WorkerReportsProgress = True
            Button1.Enabled = False
            CSVListBox.Items.Clear()
            BGW.RunWorkerAsync()
        End If
    End Sub

    Private Sub BGW_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGW.DoWork
        Dim StartingPath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        Dim File As String = "*.csv"
        FindFiles(StartingPath, File)
    End Sub

    Private Sub FindFiles(ByVal Path As String, ByVal File As String)
        Try
            Dim di As New System.IO.DirectoryInfo(Path)
            Try
                For Each fi As System.IO.FileInfo In di.GetFiles(File)
                    BGW.ReportProgress(-1, fi)
                Next
            Catch ex As Exception
            End Try
            Try
                For Each SubDI As System.IO.DirectoryInfo In di.GetDirectories
                    FindFiles(SubDI.FullName, File)
                Next
            Catch ex As Exception
            End Try
        Catch ex As Exception
        End Try
    End Sub

    Private Sub BGW_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BGW.ProgressChanged
        CSVListBox.Items.Add(e.UserState)
    End Sub

    Private Sub BGW_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BGW.RunWorkerCompleted
        MessageBox.Show("Done Searching!")
        Button1.Enabled = True
    End Sub

End Class

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Something like:

    Private Sub BGW_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BGW.ProgressChanged
        Dim fi As System.IO.FileInfo = CType(e.UserState, System.IO.FileInfo)
        Dim lvi As New ListViewItem(fi.Name)
        lvi.SubItems.Add(fi.DirectoryName)
    End Sub

Author

Commented:
Thanks alot I'm struggling to grasp the concepts of this background worker im trying to get cancel and the progressbar going I made another topic hopefully my last !

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_25104506.html

Author

Commented:
I modified the code a little put in

 BGW.WorkerSupportsCancellation = True
ProgressBar1.Value = 0

on the start of the button, also put in
ProgressBar1.Value = e.ProgressPercentage
 on  BGW_ProgressChanged

and to cancel i want it to happen on
        Private Sub ShowCSVFiles_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        BGW.CancelAsync()
    End Sub

Just not sure how to make it all work in the DoWork() method, I'm assuming that's how I would have to approach it

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial