Results 1 to 6 of 6

Thread: How do I use LB_FINDSTRING

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Northern Maine
    Posts
    281

    How do I use LB_FINDSTRING

    I am doing a little application that needs the ability to search a listbox and I found LB_FINDSTRING might help me do so. I also would like to eliminate anything that does not partain to the search criteria which would be entered into a textbox. And as I back space I would like any thing that was eliminated to be put back into the listbox.

    For example:

    I have 306 Items in the list box. Two of wich are "earthmate" and "eartha".

    If I type "earth" in the text box it will display both earthmate and eartha. However, if I type "earthm" only "earthmate" will be displayed in the listbox. What I want to do is delete the "m" from the textbox and get both eartha and earthmate displayed in the listbox again. Also if I clear the contents of the textbox I would like the listbox to be repopulated.

    If you have any questions feel free to ask.

  2. #2
    jim mcnamara
    Guest
    It works with a null-terminated string
    Using your example finding all the 'earth' entries.
    PS: LB_FINDSTRING is not case sensitive.
    Code:
    Const LB_ERR = (-1)
    
    Const LB_FINDSTRING = &H18F
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As _
    Long, ByVal wParam As Long, ByVal lParam As String) As Long
    
    ' note the change in the SendMessage declaration
    
    Dim MySels() as integer
    dim tmp as string
    dim retval as long
    tmp = "earth" & chr(0)
    retval = 0
    where = 0
    Do While retval <> LB_ERR
          retval = SendMessage(List1.hWnd,LB_FINDSTRING,where,tmp)
          if retval <> LB_ERR then
                  Redim Preserve MYSels(Ubound(MySels) + 1)
                  MySels(Ubound(MYSels) ) = retval
                  where = retval +1
          End if
    Loop
    The array MySels() will have the index values of each item that starts with 'earth'.

  3. #3
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    just changed the file a bit to make it select the closer text in textbox.
    Baaaaaaaaah

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Northern Maine
    Posts
    281
    That worked great now I just have one more problem, how can I make the selection go to the top of the list box??

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Northern Maine
    Posts
    281
    nevermind....I figured it out.

  6. #6
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    I have made a few changes to Jim's code so that u can store all the values in one list and only display the matches in another. It may or may not be what u want but anyways....

    FORM - TEXT1, LIST1 (invisible) LIST2
    VB Code:
    1. Const LB_ERR = (-1)
    2.  
    3. Const LB_FINDSTRING = &H18F
    4. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As _
    5. Long, ByVal wParam As Long, ByVal lParam As String) As Long
    6.  
    7. Private Sub Form_load()
    8.     Me.Show
    9.     List1.Visible = False
    10. 'Sample data
    11.     For x = 1 To 4
    12.         List1.AddItem Chr$(x + 64)
    13.     Next
    14.     List1.AddItem "earthmate"
    15.     For x = 1 To 4
    16.         List1.AddItem Chr$(x + 68)
    17.     Next
    18.     List1.AddItem "earth"
    19.     For x = 1 To 4
    20.         List1.AddItem Chr$(x + 72)
    21.     Next
    22.     Repopulate
    23. End Sub
    24.  
    25. Private Sub Text1_Change()
    26.     If Len(Text1.Text) = 0 Then
    27.         Repopulate
    28.     Else
    29.         FindMatches Trim$(Text1.Text)
    30.     End If
    31. End Sub
    32.  
    33. Private Sub FindMatches(ByVal SearchText As String)
    34.     Dim StopRepeat As Long 'Check to prevent endless looping
    35.     Dim UpBound As Long 'upper bound of matches
    36.     Dim x As Long 'counter
    37.     Dim MySels() As Integer
    38.     Dim retval As Long
    39.     retval = 0
    40.     Where = 0
    41.     StopRepeat = 0
    42.     UpBound = -1
    43.     Do While retval <> LB_ERR
    44.         retval = SendMessage(List1.hwnd, LB_FINDSTRING, Where, SearchText)
    45.         If retval <= StopRepeat Then retval = LB_ERR
    46.         If retval <> LB_ERR Then
    47.             UpBound = UpBound + 1
    48.             ReDim Preserve MySels(UpBound)
    49.             MySels(UpBound) = retval
    50.             If UpBound = 0 Then StopRepeat = retval
    51.             Where = retval
    52.         End If
    53.     Loop
    54.  
    55.     'Fill second list with matches
    56.     With List2
    57.         .Clear
    58.         If Where > 0 Then
    59.             For x = 0 To UBound(MySels)
    60.                 .AddItem List1.List(MySels(x))
    61.             Next
    62.         End If
    63.         If .ListCount > 0 Then .ListIndex = 0
    64.     End With
    65. End Sub
    66.  
    67. Private Sub Repopulate()
    68.     'No search term so refill
    69.     With List2
    70.         .Clear
    71.         For x = 0 To List1.ListCount - 1
    72.             .AddItem List1.List(x)
    73.         Next
    74.         If .ListCount > 0 Then .ListIndex = 0
    75.     End With
    76. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

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