Results 1 to 5 of 5

Thread: [RESOLVED] Listview Advanced Search

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    353

    Resolved [RESOLVED] Listview Advanced Search

    Hi guys,

    Im kinda stuck ....

    I would like to show all the results found in a listview control.

    So far , what im doing is in the code below. This code searches the text from txtSearch when i press the enter key, and it does the search work better then the original listview search function.

    The bad part? Well it does give only 1 result, the first encountered.

    I thought of adding a for next loop but im not sure if that would be a correct thing to do .. then something came up to my mind ...

    Is is possible to somehow...get all the results, clear the listview and populate it with the results or adding a new listview where the results would be posted?


    Cheers!

    Code:
    If KeyAscii = vbKeyReturn Then
    KeyAscii = 0
    Dim SearchString As String
          Dim oItem As ListItem
          Dim oSubItem As ListSubItem
        '  SearchString = InputBox("", "Search", "Enter Customer Name")
        SearchString = txtSearch.Text
          For Each oItem In ListView1.ListItems
              If InStr(1, oItem.Text, SearchString, vbTextCompare) Then
                  oItem.Selected = True
                  oItem.EnsureVisible
                  ListView1.SetFocus
                  Exit For
              Else
                  For Each oSubItem In oItem.ListSubItems
                      If InStr(1, oSubItem.Text, SearchString, vbTextCompare) Then
                          oItem.Selected = True
                          oItem.EnsureVisible
                          ListView1.SetFocus
                          Exit For
                      End If
                  Next
                  If Not oSubItem Is Nothing Then Exit For
              End If
          Next
    End If
    Thanks for helping me out.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Listview Advanced Search

    The reason it is exiting after 1st match is because of your Exit For statements. Let's say 2 matches were found, 1 at the top and 1 at the bottom. The user isn't going to see the 1 at the top because the listview will be scrolled all the way to the bottom when that match was found (oItem.EnsureVisible).

    You might want to figure out how you want to handle this problem. A couple ways

    1) Show first, next, previous match buttons or menu. Requires more code, but the idea is simple enough. If first match to be shown, start at top and stop when found. If next match to be shown, start on the item after the last match and continue searching. If previous match to be shown, start at position just before last match and continue search backwards.

    2) As each match is found, add it to something (collection, array, listbox, something else) and then show the user the matches. What you use to show them is up to you. You don't necessarily need to track the entire listitem/subitems, just the listitem index. Your display code can use that index to extract the info from the listivew when the time comes.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    353

    Re: Listview Advanced Search

    Thanks you very much for your kind reply.

    I ll first try with the second method becauase i think the first one could be tricky for me hehe.

    Thanks, ill report tomorrow...good night.
    Thanks for helping me out.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    353

    Re: Listview Advanced Search

    What i did is...removed this :

    oItem.Selected = True
    oItem.EnsureVisible
    ListView1.SetFocus
    Exit For

    added:

    Dim list_item As ListItem

    Set list_item = ListView2.ListItems.Add(, , oItem.Text)
    list_item.SubItems(1) = oItem.SubItems(1)


    LaVople (maybe you are italian ) thanks for the help!
    Thanks for helping me out.

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Listview Advanced Search

    Quote Originally Posted by batori View Post
    LaVople (maybe you are italian ) thanks for the help!
    Part of me is. My English last name can be translated to Italian as la volpe.
    Last edited by LaVolpe; Jan 9th, 2011 at 09:34 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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