-
Jan 7th, 2011, 08:08 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Jan 7th, 2011, 08:14 PM
#2
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.
-
Jan 7th, 2011, 10:01 PM
#3
Thread Starter
Hyperactive Member
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.
-
Jan 9th, 2011, 08:08 PM
#4
Thread Starter
Hyperactive Member
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.
-
Jan 9th, 2011, 09:18 PM
#5
Re: Listview Advanced Search
Originally Posted by batori
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|