Results 1 to 3 of 3

Thread: Searching for words in string

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    25

    Searching for words in string

    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).
    Last edited by Syruz; May 1st, 2008 at 04:14 PM.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Searching for words in string

    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.

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Searching for words in string

    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

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