Link to home
Start Free TrialLog in
Avatar of disrupt
disruptFlag for United States of America

asked on

Visual Basic 2008 BackgroundWorker Listview

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

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of disrupt

ASKER

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 !

https://www.experts-exchange.com/questions/25104506/Visual-Basic-2008-BackgroundWorker-Cancel-Progress.html
Avatar of disrupt

ASKER

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