Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search in multiselect listbox 1

Status
Not open for further replies.

wysiwygGER01

Programmer
Joined
Feb 18, 2009
Messages
188
Location
AU
Hi,

I've got a form with a list box and a text field in VB6.
I want to implement a search-as-you-type function.

My code below works just as it should but only if the list box is not multi-select.

I don't understand why this won't work with a multi select list box.
Any help would be much appreciated...

Private Sub txtfind_Change()
Dim x, z
RequisitionList.ListIndex = -1
For x = 0 To RequisitionList.ListCount - 1
RequisitionList.ListIndex = x
z = RequisitionList.Text
If LCase(Left(RequisitionList.Text, Len(txtfind.Text))) = LCase(txtfind.Text) Then
Exit Sub
End If
Next x

End Sub
 
Try this:
___
[tt]
Private Sub txtfind_Change()
Dim N As Long
For N = 0 To RequisitionList.ListCount - 1
RequisitionList.Selected(N) = InStr(1, RequisitionList.List(N), txtfind.Text, vbTextCompare) = 1
Next
End Sub[/tt]
___

Note that this code matches only those items which contain the search word at the start (similar to your code).
If you want to match all items which contain the search word, remove = 1 from the above code.
 
Hi Hypetia,

thanks for the quick reply.
Your code does almost what I want.
After I searched for 1 item in the list, it gets selected.
Is there a way to then search for a second item in this list without loosing my previous selection again?
 
You can type multiple words in txtFind separated by spaces, the following code selects all items that match any of the words.
___
[tt]
Private Sub txtfind_Change()
Dim N As Long, Items() As String, Item As Variant
Items = Split(txtfind.Text)
For N = 0 To RequisitionList.ListCount - 1
RequisitionList.Selected(N) = False
For Each Item In Items
If Len(Item) Then
If InStr(1, RequisitionList.List(N), Item, vbTextCompare) = 1 Then
RequisitionList.Selected(N) = True
Exit For
End If
End If
Next
Next
End Sub[/tt]
___

I would suggest that instead of highlighting/selecting the matching words, you remove all list items that do not match the search criteria. This will help the user find the matching words easily as the list shrinks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top