Results 1 to 4 of 4

Thread: ListBox Question?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2001
    Location
    Southern California
    Posts
    733

    ListBox Question?

    How do you search a listbox character by character and make the result the 1st position in the listbox. In other words, the user types "c" and the first listbox item that contains a "c" as the first character will be displayed. If the user types "ch", the first occurrence will be displayed in position 1.

    Thanks,
    Jeff

  2. #2
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    While you can code this, you could try the combo box in forms 2.0 library. As I recall it does this already, but I could be wrong.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2001
    Location
    Southern California
    Posts
    733
    That does work. But I'm confused on one thing.

    When I try to enter a value that is not contained within the list, it won't let me continue entering data. I don't know if this is because of the settings or if it has something to do with the KeyPress event.

    Any ideas?

    thanks again,
    Jeff

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    The following code works for a list box. The code would be a little bit simplier if you would use a combo box since you don't have to remember the last keystokes (you only need to send the Text property)
    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 Declare Function GetTickCount _
     Lib "kernel32" () As Long
    
    Private Const LB_FINDSTRING = &H18F
    
    Private Sub List1_KeyPress(KeyAscii As Integer)
        Dim nCurrentTime As Long
        Dim nRetVal As Long
        Static nTime As Long
        Static sTxt As String
        
        nCurrentTime = GetTickCount
        If nCurrentTime - nTime > 1000 Then
            nTime = GetTickCount
            sTxt = ""
        End If
        Select Case KeyAscii
            Case vbKeyBack
                If Len(sTxt) Then
                    sTxt = Left$(sTxt, Len(sTxt) - 1)
                End If
            Case Else
                sTxt = sTxt & Chr$(KeyAscii)
        End Select
        If Len(sTxt) Then
            nRetVal = SendMessage(List1.hwnd, LB_FINDSTRING, 0, ByVal sTxt)
            List1.ListIndex = nRetVal
        End If
        Debug.Print sTxt
    End Sub
    Best regards

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