Results 1 to 4 of 4

Thread: [RESOLVED] search listbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Resolved [RESOLVED] search listbox

    can anybody show me how to do a binary search in a sorted listbox ?

    thanks.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: search listbox

    Not sure if this is Binary, but it is the quickest way to search it.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _
    4. (ByVal hWnd As Long, _
    5. ByVal wMsg As Long, _
    6. ByVal wParam As Long, _
    7. ByVal lParam As String) As Long
    8. Private Const CB_FINDSTRINGEXACT = &H158
    9. Private Const LB_FINDSTRINGEXACT = &H1A2
    10. Public newitem As String
    11.  
    12.  
    13. Sub searchlist()
    14.   Dim lngRetVal As Long
    15.   lngRetVal = SendMessageString(List1.hWnd, _
    16.               LB_FINDSTRINGEXACT, -1&, _
    17.               newitem)
    18.   If lngRetVal = -1& Then
    19.     List1.AddItem newitem
    20.   Else
    21.     List1.ItemData(lngRetVal) = List1.ItemData(lngRetVal) + 1
    22.     MsgBox List1.ItemData(lngRetVal)
    23.     ' It's already in the list and lngRetVal is the listindex
    24.   End If

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: search listbox

    thanks but its not what i am after.

    i am doing a listbox usercontrol and i want to do a sorted sub so when i add a new item i need to know which index to put it so i know the binary search will be the quickest but i dont know how to do it.

    thanks.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: search listbox

    ok, i got it.

    thanks.

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