Home | What's New | Articles | Code Downloads | Tool Box | Links | Code Snippets  

Combo Box

Creating an Auto-Completing Combo Box

This code finds the closest match to what is enter into a combo box

Enter this into the General Declarations of a form:

Dim BackSpacedPressed As Boolean
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_ERR = (-1)
Private Const CB_FINDSTRING = &H14C
Private Sub cboBox_Change()
    Dim LengthofText As Integer
    Dim StrToFind As String
    Dim Pos As Long
    Dim SelStart As Integer
    'If backspace was pressed let the normal thing happen    
    If BackSpacedPressed Then Exit Sub
    StrToFind = cboBox.Text
    SelStart = cboBox.SelStart 'Save the starting point
    Pos = SendMessage(cboBox.hwnd, CB_FINDSTRING, -1, ByVal StrToFind)
    If Pos <> CB_ERR Then 'If item found
        'Place the auto-completed text in the text area
        cboBox.Text = cboBox.List(Pos)
        cboBox.SelStart = SelStart 'Place the caret at the correct position
        LengthofText = Len(cboBox.Text)
        If LengthofText > SelStart Then
            'Highlight the auto-completed text so it can
            'be deleted quickly if wanted
            cboBox.SelLength = LengthofText - SelStart 
        End If
    End If
End Sub
Private Sub cboBox_KeyPress(KeyAscii As Integer)
    'Remember if last key pressed was the backspace
    BackSpacedPressed = (KeyAscii = vbKeyBack)
End Sub

Add a combo box to the form called "cboBox" or rename all instances of "cboBox" to match the name or your combo box.


Setting the dropdown width of a combo box 

Enter this into the General Declarations of a form:

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_GETDROPPEDWIDTH = &H15F
Private Const CB_SETDROPPEDWIDTH = &H160

Add this to the same form or module:

Public Property Let ComboDropDownWidth(m_cboBox As ComboBox, _
ByVal NewComboDropDownWidth As Long)
    SendMessage m_cboBox.hwnd, CB_SETDROPPEDWIDTH, _NewComboDropDownWidth, 0
End Property

Public Property Get ComboDropDownWidth(m_cboBox As ComboBox) As Long
    ComboDropDownWidth = SendMessage(m_cboBox.hwnd, CB_GETDROPPEDWIDTH, 0, 0)
End Property

To set the width of a combo box's dropdown list use:

    ComboDropDownWidth(MyComboBox) = 500 ' Pixels


Setting the combo box Extended User Interface

By default pressing f4 opens and closes the dropdown list.  When the extended user interface is set pressing the down arrow does this instead of f4. 

Enter this into the General Declarations of a form:

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_SETEXTENDEDUI = &H155

To turn on the Extended User Interface use:

SendMessage cboBox.hwnd, CB_SETEXTENDEDUI, 1, 0

To turn it off use:

SendMessage cboBox.hwnd, CB_SETEXTENDEDUI, 0, 0