ComboBox Control (VBCCRComboBox)
The VBCCRComboBox control is a combination box control that combines the functionality of a text box and a drop-down list box. Users can select an item from the list or directly enter values in the text box.
Styles
ComboBox has three main styles:
vbComboDropdown(0) - Default style, includes an editable text box and drop-down listvbComboSimple(1) - Combo box with permanently visible list boxvbComboDropdownList(2) - Combo box with drop-down list only, text box portion is not editable
Properties
Appearance Properties
Style- Combo box style (0-2)Text- Text in the text boxBackColor- Background colorForeColor- Foreground colorFont- Font propertiesEnabled- Whether enabledVisible- Whether visibleHeight- Control heightWidth- Control widthLeft- Left marginTop- Top margin
List Properties
List- Array of list itemsListCount- Number of list itemsListIndex- Index of currently selected itemItemData- Numeric data associated with list itemsSorted- Whether automatically sortedMaxLength- Maximum number of characters in text boxSelLength- Length of selected textSelStart- Starting position of selected textSelText- Selected textNewIndex- Index of most recently added item
Events
Change- Triggered when text changesClick- Triggered when clickedDblClick- Triggered when double-clickedKeyDown- Triggered when key is pressed downKeyPress- Triggered when key is pressedKeyUp- Triggered when key is releasedScroll- Triggered when scrolledDropDown- Triggered when drop-down list is displayedCloseUp- Triggered when drop-down list is closed
Methods
AddItem- Add list itemRemoveItem- Remove list itemClear- Clear listRefresh- Refresh displaySetFocus- Set focus
Code Examples
Basic Usage
vb
Private Sub InitComboBox()
With ComboBox1
.Style = vbComboDropdown ' Set style
.Clear ' Clear list
' Add list items
.AddItem "Option1"
.AddItem "Option2"
.AddItem "Option3"
.ListIndex = 0 ' Select first item
End With
End SubAuto-Complete Input
vb
Private Sub ComboBox1_KeyPress(KeyAscii As Integer)
Static LastLen As Long
Dim StartPos As Long
Dim FindText As String
Dim i As Long
' When Enter key is pressed
If KeyAscii = vbKeyReturn Then
KeyAscii = 0 ' Cancel key press
Exit Sub
End If
' When Backspace key is pressed
If KeyAscii = vbKeyBack Then
LastLen = Len(ComboBox1.Text) - 1
If LastLen < 0 Then LastLen = 0
Exit Sub
End If