Results 1 to 12 of 12

Thread: Searching a Listbox.................

  1. #1

    Thread Starter
    Addicted Member Smie's Avatar
    Join Date
    Jun 1999
    Location
    Columbus, OH
    Posts
    249

    Question

    Im trying to set a timer, to highlight a "name" in my listbox and check if it matches the search text in text1.text, once it finds it, i want it to select the name, and im going to have it preform an action......

    my problem is that i cant figure out how to have it scroll through the names list, highlight and move down, ive figured out how to determine what the selected text says (list1.text) but im still stuck, please help!

  2. #2
    Guest
    This will search a list and highlight the item.

    Code:
    'Used:  Textbox, Listbox, CommandButton
    'Its best if you search lowercase so when adding items:
    'List1.additem Lcase(item)
    'Textbox is the searchbox
    
    Private Sub Command_Click()
    On Error Resume Next
    Screen.MousePointer = 11
    Text1.Text = LCase(Text1.Text)
    List1.ListIndex = -1
    For i = 0 To List1.ListCount
    List1.ListIndex = List1.ListIndex + 1
    If List1 = Text1.text Then
    Screen.MousePointer = 0
    Exit Sub
    End If
    Next i
    Screen.MousePointer = 0
    End Sub

  3. #3

    Thread Starter
    Addicted Member Smie's Avatar
    Join Date
    Jun 1999
    Location
    Columbus, OH
    Posts
    249
    the On Error Resume Next is not working, i dont understand why, if there is no match, it errors here:

    List1.ListIndex = List1.ListIndex + 1


    thank you i appreciate your help!


  4. #4
    Addicted Member Dim A's Avatar
    Join Date
    Jul 2000
    Posts
    201
    Originally posted by Smie
    the On Error Resume Next is not working, i dont understand why, if there is no match, it errors here:

    List1.ListIndex = List1.ListIndex + 1


    thank you i appreciate your help!

    If there's no match, then it goes one past the end of the list. Why on Error resume next doesn't work, i'm not sure, but that is the cause for the error.

    Changing
    Code:
    For i = 0 To List1.ListCount
    To...
    Code:
    For i=0 To List1.ListCount - 1
    Should fix it.

    If there's no match you should be able to set List1.ListIndex equal to -1 to deselect items in the listbox.

  5. #5
    Addicted Member Dim A's Avatar
    Join Date
    Jul 2000
    Posts
    201

    Side Note:

    Originally posted by Matthew Gates
    This will search a list and highlight the item.

    Code:
    For i = 0 To List1.ListCount
       List1.ListIndex = List1.ListIndex + 1
    
       If List1 = Text1.text Then
          Screen.MousePointer = 0
          Exit Sub
       End If
    
    Next i
    Won't this skip the first item in the list? I thought listbox Indexes started at 0, but by the time this does the comparison, the index will already be 1.


  6. #6
    Guest
    That's where the List1.ListIndex = -1 comes in.

    The On Error Resume Next errors because when List1.ListIndex = List1.ListIndex + 1 comes to the end of the listbox, it can't plus one anymore, so it creates an error, and thats where the On Error Resume Next comes in.

  7. #7

    Thread Starter
    Addicted Member Smie's Avatar
    Join Date
    Jun 1999
    Location
    Columbus, OH
    Posts
    249
    ok, thank you for all your help, now after it finds a match, how would i make the listbox select the match?

  8. #8
    Guest
    Did you try it?

    Code:
    Private Sub Command_Click()
    On Error Resume Next 'Skip error
    Screen.MousePointer = 11 'Hourglass
    Text1.Text = LCase(Text1.Text) 'Lowercase Search string
    List1.ListIndex = -1 'Select negative number so it starts at 0 when searching
    For i = 0 To List1.ListCount 'count the listbox
    List1.ListIndex = List1.ListIndex + 1 'highlight and search listbox
    If List1 = Text1.text Then 'If List1 = Searchstring Then...
    Screen.MousePointer = 0 'Arrow
    Exit Sub 'Stop leaving highlighted item
    End If 'End the If
    Next i 'Loop to next item until a search it found
    Screen.MousePointer = 0 'If search wasn't found, the hourglass will stay but we want it back to the arrow
    List1.ListIndex = -1 'unhighlight the list if nothing was found
    End Sub
    It should highlight the found item. That is the code I use for this directory searcher I made and it works very well.

  9. #9

    Thread Starter
    Addicted Member Smie's Avatar
    Join Date
    Jun 1999
    Location
    Columbus, OH
    Posts
    249
    Thanks a bunch, it worked!, but just as one final question, how would i have just a separate command button highlight say, the 5th object in the list? like you click the button and it selects the 5th object on the list.....i really appreciate all of your help today, thank you!

  10. #10
    Guest
    To highlight any item (say the 5th item):

    Code:
    List1.ListIndex = 5
    You could also highlight the name:

    Code:
    List1 = "<name>"

  11. #11
    Guest
    Try this.

    Code:
    Private Sub Command1_Click()
    
        'Change strTemp to the text you want to search for
        strTemp = "MyTextHere"
        
        For i = 0 To List1.ListCount - 1
            List1.ListIndex = i
            If UCase(List1.Text) = UCase(strTemp) Then Exit For Else List1.ListIndex = -1
        Next i
            
    End Sub

  12. #12
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    If you're list is going to be fairly long, you'll find the API a faster way to search the listbox, i.e.
    Code:
    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         ' Partial Match
    Private Const LB_FINDSTRINGEXACT = &H1A2    ' Exact Match
    
    Private Sub Text1_Change()
        Dim sFind As String
        sFind = Text1
        List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, 0&, ByVal sFind)
    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