What would be the fastest way to search in long string for any word from a list of words in a listbox?
Is there a faster way than just searching the whole text for Listbox.List(0), then Listbox.List(0), etc (in a for loop obviously).
Printable View
What would be the fastest way to search in long string for any word from a list of words in a listbox?
Is there a faster way than just searching the whole text for Listbox.List(0), then Listbox.List(0), etc (in a for loop obviously).
To find all items containing some text you will have to loop.
If you only need first occurence then using SendMessage api and LB_FINDSTRINGEXACT or LB_FINDSTRING is the fastest method.
Samples for either approach are posted throughout the forum.
Here is a quick sample that will highlight all items that contain text (I'm using textbox):
Code:Option Explicit
Private Declare Function SendMessage _
Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lparam As Any) As Long
Private Const LB_FINDSTRING = &H18F
Private Sub btnFindAll_Click()
'===========================
Dim iIndex&, iTemp&, sText$
sText = Text1.Text
iCurIndex = 0
iIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal sText)
If iIndex >= 0 Then
List1.Selected(iIndex) = True
iTemp = iIndex
Do While iIndex > -1
iIndex = SendMessage(List1.hwnd, LB_FINDSTRING, ByVal iIndex, ByVal sText)
List1.Selected(iIndex) = True
If iIndex = iTemp Then Exit Do
Loop
End If
End Sub