School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,363 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,444 people online right now. Registration is fast and FREE... Join Now!




Autocomplete combobox

 
Reply to this topicStart new topic

> Autocomplete combobox, How to make a combobox with large number of items to select item autma

muddasir
Group Icon



post 15 Oct, 2007 - 11:29 AM
Post #1


if we have small no of items in a combobox we
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
form .

CODE
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
of text

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
it do.



CODE
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

CODE
'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.


CODE
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.








Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

akhileshbc
Group Icon



post 28 Sep, 2008 - 02:58 AM
Post #2
If you set the Style property of combo box to 2-Dropdown, then you can cycle through the first letter of the item. But your code is much useful....smile.gif Thanks yaar..smile.gif
Go to the top of the page
+Quote Post

mashuaibu
*



post 17 Dec, 2008 - 05:30 AM
Post #3
QUOTE(akhileshbc @ 28 Sep, 2008 - 02:58 AM) *

If you set the Style property of combo box to 2-Dropdown, then you can cycle through the first letter of the item. But your code is much useful....smile.gif Thanks yaar..smile.gif

Hi Muddasir,

I went through your combo box write-up (i.e. code and instructions). It was great. I will apply it and see how it will work. I have been looking for something like it. Thanks so much.

Best regards.

Al Amin.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 08:37PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month