That's the code i'm working with.
What a want to do is that when i adds "items" i want it to read the item's string and compare it with the text in the combo box, and if both texts are the same, i want the search to stop and display a msgbox with "file found".Code:Imports System.IO Imports System.ComponentModel Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, _ ByVal e As EventArgs) Handles MyBase.Load 'Display a list of available drives in the ListBox. Me.srDrives.DataSource = DriveInfo.GetDrives() End Sub Private Sub srFind_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles srFind.Click 'Get the selected drive path. Dim drive As String = Me.srDrives.Text 'Start searching the drive for files asynchronously. Me.BackgroundWorker1.RunWorkerAsync(drive) End Sub Private Sub srCancel_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles srCancel.Click 'Cancel the asynchronous search. Me.BackgroundWorker1.CancelAsync() End Sub Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _ ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim drive As String = CStr(e.Argument) Dim worker As BackgroundWorker = DirectCast(sender, BackgroundWorker) 'Initiate the recursive file search. 'If GetFiles returns False it means the search was cancelled. e.Cancel = Not Me.GetFiles(drive, worker) End Sub Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _ ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged 'Get the items passed from the secondary thread. Dim items As ListViewItem() = DirectCast(e.UserState, ListViewItem()) 'Add the items to the list. Me.srList.Items.AddRange(items) End Sub Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _ ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted If e.Cancelled Then 'The search was cancelled before completing. MessageBox.Show("Operation cancelled") Else 'The search completed normally. MessageBox.Show("Operation complete") End If End Sub Private Function GetFiles(ByVal folder As String, ByVal worker As BackgroundWorker) As Boolean Dim cancelled As Boolean = False If worker.CancellationPending Then 'The user has requested that the search be cancelled. cancelled = True Else Try 'Get the files in the current folder. Dim files As String() = Directory.GetFiles(folder) Dim upperBound As Integer = files.GetUpperBound(0) Dim items(upperBound) As ListViewItem Dim file As String 'Create a ListViewItem for each file. For index As Integer = 0 To upperBound file = files(index) items(index) = New ListViewItem(New String() {Path.GetFileName(file), _ Path.GetDirectoryName(file)}) Next 'Pass the items to the UI thread to be displayed. worker.ReportProgress(0, items) 'Search each subfolder. For Each subfolder As String In Directory.GetDirectories(folder) 'This is the recursive call, i.e. the method calls itself. If Not Me.GetFiles(subfolder, worker) Then 'The user has requested that the search be cancelled. cancelled = True Exit For End If Next Catch ex As Exception 'The folder is inaccessible. Ignore and continue. End Try End If 'The sub-search completed if it was not cancelled. Return Not cancelled End Function End Class
And if it's posible, to erase the other items that don't match the text in the combobox.
But I don't know how to do that.
THANKS!




Reply With Quote