Results 1 to 6 of 6

Thread: Auto-completing combo box

  1. #1

    Thread Starter
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    I found this function somewhere a while ago;

    Stick this in a module;

    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
    
    Const CB_FINDSTRING = &H14C
    Const CB_ERR = (-1)
    
    Public Function AutoFind(ByRef cboCurrent As ComboBox, ByVal KeyAscii As Integer, Optional ByVal LimitToList As Boolean = False)
            
    Dim lCB As Long
    Dim sFindString As String
    
    On Error GoTo Err_Handler
        If KeyAscii = 8 Then
            If cboCurrent.SelStart <= 1 Then
                cboCurrent = ""
                AutoFind = 0
                Exit Function
            End If
            If cboCurrent.SelLength = 0 Then
                sFindString = UCase(Left(cboCurrent, Len(cboCurrent) - 1))
            Else
                sFindString = Left$(cboCurrent.Text, cboCurrent.SelStart - 1)
            End If
        ElseIf KeyAscii < 32 Or KeyAscii > 127 Then
            Exit Function
        Else
            If cboCurrent.SelLength = 0 Then
                sFindString = UCase(cboCurrent.Text & Chr$(KeyAscii))
            Else
                sFindString = Left$(cboCurrent.Text, cboCurrent.SelStart) & Chr$(KeyAscii)
            End If
        End If
        lCB = SendMessage(cboCurrent.hwnd, CB_FINDSTRING, -1, ByVal sFindString)
    
        If lCB <> CB_ERR Then
            cboCurrent.ListIndex = lCB
            cboCurrent.SelStart = Len(sFindString)
            cboCurrent.SelLength = Len(cboCurrent.Text) - cboCurrent.SelStart
            AutoFind = 0
        Else
            If LimitToList = True Then
                AutoFind = 0
            Else
                AutoFind = KeyAscii
            End If
        End If
    Err_Handler:
    End Function

    Then, in the KeyPress event of the combobox in question - do this

    Code:
    Private Sub cboText_KeyPress(KeyAscii As Integer)
    
    KeyAscii = AutoFind(cboText, KeyAscii, False)
    
    End Sub
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  2. #2

    Thread Starter
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Oh, and if you want to limit the user to the items in the combobox you use

    Code:
    Private Sub cboText_KeyPress(KeyAscii As Integer)
    
    KeyAscii = AutoFind(cboText, KeyAscii, True)
    
    End Sub
    instead
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  3. #3
    Hyperactive Member DaveR's Avatar
    Join Date
    Mar 2001
    Location
    Ireland
    Posts
    268

    combo box

    Thats exactly what I was looking for,
    cheers



    By any chance do you know if there is a way to open the combo box automatically when you start typing text in.
    i.e. instead of pressing the down arrow to open it.

    At present , when you open the combo by pressing the down "arrow" , it opens fine , but when you select an item the combo closes again.

    any ideas...??
    DaveR

  4. #4
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    Code:
    Private Const CB_SHOWDROPDOWN = &H14F
    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 CB_GETDROPPEDSTATE = &H157
    
    Private Sub cmd1_Click()
    Dim lRet&
    lRet = SendMessage(cbo1.hwnd, CB_SHOWDROPDOWN, Not SendMessage(cbo1.hwnd, CB_GETDROPPEDSTATE, 0, 0), 0)
    End Sub
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  5. #5
    Hyperactive Member DaveR's Avatar
    Join Date
    Mar 2001
    Location
    Ireland
    Posts
    268
    thats works well , but when you press enter after your selected text appears , which a lot of people would instinctively do , the combo opens up , when you really want it to go down..

    its not a major issue ....
    DaveR

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Originally posted by DaveR
    thats works well , but when you press enter after your selected
    text appears , which a lot of people would instinctively do , the
    combo opens up , when you really want it to go down..
    Thats easily addressed DaveR.
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.  
    3.       If KeyAscii = 13 Then 'user hit enter key
    4.          Dim lRet&
    5.          lRet = SendMessage(Combo1.hwnd, CB_SHOWDROPDOWN, 1, ByVal 0&)
    6.     End If
    7.  
    8. 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