can easily select an item from it , but if the item
list it too long we want that if we can start typing
any word it will autmatic be selected in combobox.
which infect is not possible as default in a combobox.
to achieve this we have to do a little bit of coding.
i will try it to explain to you my best
first of all we have to place a comboxbox in the
form combobox1. set the style property of the combobox as 0 dropdown combo
first we have to add items in the combobox for
this we have to add items in the load event of the
Private Sub Form_Load() combobox1.AddItem ("Apple") combobox1.AddItem ("Banana") combobox1.AddItem ("Lemon") combobox1.AddItem ("Orange") combobox1.AddItem ("Malta") end sub
this code fill comboxbox with items
Now for combobox to autocomplete we have to code combobox for two evnet we may change the text
by typing a new one or you may delete some portion
now for the text change we have to do the following coding in the change event of combobox.
the code also explain after each step what functions
Dim blnAuto As Boolean Dim strPart As String, iLoop As Integer, iStart As Integer, strItem As String 'don't do if no text or if change was made by autocomplete coding If Not blnAuto And combobox1.Text <> "" Then 'save the selection start point (cursor position) iStart = combobox1.SelStart 'get the part the user has typed (not selected) strPart = Left$(combobox1.Text, iStart) For iLoop = 0 To combobox1.ListCount - 1 'compare each item to the part the user has typed, '"complete" with the first good match strItem = UCase$(combobox1.List(iLoop)) If strItem Like UCase$(strPart & "*") And _ strItem <> UCase$(combobox1.Text) Then 'partial match but not the whole thing. '(if whole thing, nothing to complete!) blnAuto = True combobox1.SelText = Mid$(combobox1.List(iLoop), iStart + 1) 'add on the new ending combobox1.SelStart = iStart 'reset the selection combobox1.SelLength = Len(combobox1.Text) - iStart blnAuto = False Exit For End If Next iLoop End If
and coding for keydown evnet will be
'Unless we watch out for it, backspace or delete will just delete 'the selected text (the autocomplete part), so we delete it here 'first so it doesn't interfere with what the user expects If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Then blnAuto = True combobox1.SelText = "" blnAuto = False ElseIf KeyCode = vbKeyReturn Then 'Accept autocomplete on 'Enter' keypress combobox1_LostFocus 'the following causes the item to be selected and 'the cursor placed at the end: combobox1.SelStart = Len(combobox1.Text) 'This would select the whole thing instead: 'combobox1.SelLength = Len(combobox1.Text) 'alternatively, you could move the focus to the next control here End If
and on the lost evnet we have to code this,so as
if we type only first few letter of the item auto
fill the remaining.
Dim iLoop As Integer 'Match capitalization if item entered is one on the list If combobox1.Text <> "" Then For iLoop = 0 To combobox1.ListCount - 1 If UCase$(combobox1.List(iLoop)) = UCase$(combobox1.Text) Then blnAuto = True combobox1.Text = combobox1.List(iLoop) blnAuto = False Exit For End If Next iLoop End If
Now, Run the project in the combobox type letter
A.it shows you the complete item "Apple".
now delete and and change it to any other
word it will automatically selected.
for small list of items it may not look quiet good but
for a larege list of items its help a lot.