Results 1 to 6 of 6

Thread: list search?

  1. #1
    Guest
    Any way to search a list quickly?
    This code works, but it's slow:

    Code:
    On Error Resume Next
    List1.ListIndex = -1
    For i = 0 To List1.ListCount
    List1.ListIndex = List1.ListIndex + 1
    If List1 = Label1.Caption Then
    Msgbox "" & List1 & " - found!"
    Exit Sub
    End If
    Next i
    But is there another (faster) way?

  2. #2

  3. #3
    Guest
    Originally posted by MartinLiss
    Try this (and you won't need On Error...)
    For i = 0 To List1.ListCount - 1
    If List1.List(i)= Label1.Caption Then
    Msgbox List1.List(i) & " - found!"
    Exit Sub
    End If
    Next i
    The "On Error Resume Next" is for the List1.listindex = List1.listindex + 1 for when it hits the end of the list.
    And your code does not move the list down to search through it. It doesn't move what so ever so it would have nothing to really do. But I was wondering if there was another way to search a list box faster.

    [Edited by Matthew Gates on 05-25-2000 at 10:03 PM]

  4. #4
    Member
    Join Date
    May 2000
    Posts
    63
    Anytime you search a list/array sequentially it becomes slower every time you add an entry to the list. If the list is sorted, the best method is to use a binary search. A binary search cuts the list in half each time it does a comparison. Searching a 1000 item list sequentially, searching for the next to last element will take 999 comparisons. A binary search would take 10 comparisons, MAX!! Think about it...(numbers are rounded up)

    1000/2=500

    500/2=250

    250/2=125

    125/2=63

    63/2=32

    32/2=16

    16/2=8

    8/2=4

    4/2=2

    2/2=1

    Because the list is cut in half the same 1000 element list would only take 10 comparisons.

    If you need code to do this, I'd be happy to give it to you.




  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    You don't need to "move" a list to search it. Did you try my way? And even if you do it your way you don't need the On Error if you do it this way:

    Do Until List1.ListIndex = List1.ListCount - 1
    List1.ListIndex = List1.ListIndex + 1
    If List1 = Label1.Caption Then
    MsgBox List1 & " - found!"
    Exit Sub
    End If
    Loop

  6. #6
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up API is the best solution for you.

    I think API function is the faster solution to search a string in ListBox or ComboBox. Below just a example how I did it.

    Code:
    Option Explicit
    'NOTE:
    'Please change the lParam Datatype from Long to Any
    'Else the SendMessage API will not function correctly.
    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
    'For ComboBox
    Private Const CB_FINDSTRING = &H14C
    'For List View
    Private Const LB_FINDSTRING = &H18F
    Dim xOpt%
    
    Private Sub Command1_Click()
        Select Case xOpt
        Case 0 'ComboBox Serach
            Combo1.ListIndex = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1&, ByVal CStr(Text1.Text))
        Case 1 'ListBox Search
            List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1&, ByVal CStr(Text1.Text))
        End Select
    End Sub
    
    Private Sub Form_Load()
        With Combo1
            .AddItem "Computer"
            .AddItem "Screen"
            .AddItem "Modem"
            .AddItem "Printer"
            .AddItem "Scanner"
            .AddItem "Sound Blaster"
            .AddItem "Keyboard"
            .AddItem "CD-Rom"
            .AddItem "Mouse"
            .ListIndex = 0
        End With
        
        With List1
            .AddItem "Computer"
            .AddItem "Screen"
            .AddItem "Modem"
            .AddItem "Printer"
            .AddItem "Scanner"
            .AddItem "Sound Blaster"
            .AddItem "Keyboard"
            .AddItem "CD-Rom"
            .AddItem "Mouse"
        End With
    End Sub
    
    
    
    Private Sub Option1_Click()
        xOpt = 0
    End Sub
    
    Private Sub Option2_Click()
        xOpt = 1
    End Sub
    By the way, jmatello can I have a copy you the source that you mention? because current I working with binary data file and writing a faster text search routine to searcha particular text under WinCE platform.

    Thanks jmatello.




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