Results 1 to 8 of 8

Thread: [RESOLVED] How do i match a word in a ListViewBox?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Resolved [RESOLVED] How do i match a word in a ListViewBox?

    Hi, i'm to match a word that's written in a combobox with one in a listviewbox. I've tried with this code, but it doesn't select the word. Any thoughts on this?

    Code:
    Dim itmX As ListViewItem
    itmX = ListView1.FindItemWithText("TextToSearch", True, 0)
    itmX.Selected = True

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How do i match a word in a ListViewBox?

    You could do this instead:

    vb.net Code:
    1. ListView1.Items((ListView1.FindItemWithText("test").Index)).Selected = True

    Note though that FindItemWithText just returns the first item that begins with the text you entered, so its not really that accurate if you want to do a proper search. You would be better off looping through the Items collection and checking the Text property if you wanted to make a more accurate search
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: How do i match a word in a ListViewBox?

    I tried your code and it works great!

    Another question:

    How can i "read" the string of the items, before they are added to the listviewbox. That way if it finds the matching word i would stop and say "hey, i found it" in a msgbox.

    Oh, how do i delete all the other items from the list, BUT the one that i was looking for?

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How do i match a word in a ListViewBox?

    How can i "read" the string of the items, before they are added to the listviewbox. That way if it finds the matching word i would stop and say "hey, i found it" in a msgbox.
    Well how are you adding them to your ListView? Are you sure you mean "before they are added to the listbox" and not before they are found/selected as you wont be re-adding all of the items to the listview every time someone clicks search will you?

    Oh, how do i delete all the other items from the list, BUT the one that i was looking for?
    I assume you mean so that you can like a filter so that when someone searches for something then all of the other items dissapear - for this purpose I wish there was a Visible property of ListViewItems just like there is for DGV rows... but unfortunately there is not (or at least I havent found one!).
    So one way to remove all of the items but the one that was found from the search would be instead of using the FindItemWithText method you just loop through each item and check its text property and if its not what you were looking for then you remove it using the Remove method of the ListViewItem.

    Another way would be to use your FindItemWithText method as normal and store that ListViewItem in a variable, then use the Clear method of the ListView to remove all items, then add that item you stored back as the only item.

    As always, more than one way to skin a cat
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: How do i match a word in a ListViewBox?

    That's the code i'm working with.
    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
    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".

    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!

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How do i match a word in a ListViewBox?

    Cant you just add an IF statement into the loop in your GetFiles function that creates the listview items? Just have this IF statement test to see if the item you are about to add has the same text property as the combobox. As your doing this in a background thread you shouldn't try to access the combobox's text property directly so just pass it in as an argument to your background worker.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: How do i match a word in a ListViewBox?

    Thanks for your help =D

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How do i match a word in a ListViewBox?

    No problem, dont forget to mark this thread as resolved and of course a rating wouldn't go a miss
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width