Auto select in drop down combo box

Is there a way to select items in a combo as the user enters text. For example, if the user types 't' when the list contains 'text' then that item should be selected. If two or more items start with the same character then the next character (and so on) typed should select the more correct entry. If no characters match the entries then no selection should be made
chillAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mcriderCommented:
You can do this with the combobox object that comes with the Microsoft Forms 2.0 Object Library.

You can add it to your project by right-clicking the toolbar and selecting "Components..." then selecting "Microsoft Forms 2.0 Object Library" from the list.


A word of warning... This library is BIG.  It contains versions of almost all of the standard objects (like textbox, listbox, image, etc.)

There is also a help file for this library:  FM20.HLP

If this help file is not on your system, leave your email address and I will mail you a copy...


Give it a try...



Cheers!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chillAuthor Commented:
Although this is a solution I would rather not use FM20. Any other solutions?
0
Éric MoreauSenior .Net ConsultantCommented:
Use this code snippet with a standard Combo box.

Private Const CB_FINDSTRING = &H14C
Private Declare Function SendMessageAny _
                Lib "user32" _
                Alias "SendMessageA" _
                (ByVal hwnd As Long, _
                 ByVal wMsg As Long, _
                 ByVal wParam As Long, _
                 lParam As Any _
                ) As Long

Private Sub Combo1_Change()
Static intLeftOff As Integer
    Call ComboPosition(Combo1, intLeftOff)
End Sub

Private Sub Form_Load()
    With Combo1
        .AddItem "Austria"
        .AddItem "Brazil"
        .AddItem "Canada"
        .AddItem "Spain"
        .AddItem "United States"
    End With
End Sub

Private Sub ComboPosition(ByRef pCombo As ComboBox, ByRef pintLeftOff As Integer)
Dim intStart As Integer
Dim strString As String

    intStart = pCombo.SelStart
   
    If pintLeftOff <> 0 Then
        pCombo.SelStart = pintLeftOff
        intStart = pintLeftOff
    End If
   
    strString = CStr(Left$(pCombo.Text, intStart))
    pCombo.ListIndex = SendMessageAny(pCombo.hwnd, CB_FINDSTRING, -1, ByVal CStr(Left$(pCombo.Text, intStart)))
   
    If pCombo.ListIndex = -1 Then
        pintLeftOff = Len(strString)
        pCombo.Text = strString
    End If
   
    pCombo.SelStart = intStart
    pintLeftOff = 0
End Sub

Hope this will answers your needs!
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

chillAuthor Commented:
Mcrider's solution is on the way to solve the problem. What I did not say is that I want to use it in "dropdown list" mode. What it fails to do is to handle the case where two or more items share the same starting character. It incorrectly selects the first one only.
0
Éric MoreauSenior .Net ConsultantCommented:
Whi is it incorrect to select the first item correspondig to your typing? I consider it to be perfectly normal.

As you type more character, the selected item may change to choose the next item!
0
chillAuthor Commented:
Set the combo to 2-dropdown list.

If you enter "Spain" and "South Africa" into the list and then hit 'S' then Spain is selected. If you now hit 'o' then the selection is not changed from Spain to South Africa as I would have expected. To get to South Africa you have to type a second 's'.

If the list is in dropdown combo the the behaviour is changed as you type the 'o' then South Africa is selected.
0
mcriderCommented:
chill

In my solution, if you set the MatchEntry Property, to FmMatchEntryComplete, it does Extended matching. As each character is typed, the control searches for an entry matching all characters entered.

Cheers!
0
Éric MoreauSenior .Net ConsultantCommented:
Oh yeah cause if it is combo style, then the code given won't work. The default properties of the combo (first character selection) will occur.
0
mcriderCommented:
emoreau,

It doesn't matter which style of combo box you use, my code STILL works...

If you use the style "fnStyleDropDownList", and you have done the following:

     ComboBox1.AddItem "John"
     ComboBox1.AddItem "John Smith"
     ComboBox1.AddItem "John Adams"

when you focus on the combobox and type "J", John will show up...

but if you keep typing "ohn " then John Smith will show up...

if you keep typing "A" then John Adams will show up...


Cheers!
0
Éric MoreauSenior .Net ConsultantCommented:
mcrider,

the guy doesn't want to use Forms!
0
mcriderCommented:
emoreau,

After chill said "Although this is a solution I would rather not use FM20..."

Chill said "Mcrider's solution is on the way to solve the problem. What I did not say is that I want to use it in "dropdown list" mode."

My last comment was addressing that concern.


Cheers!
0
chillAuthor Commented:
Please forgive me guys. Mcrider is correct. I do not want to use Forms. Incorrectly I referred to the incorrect solution in my previous posting.

Sorry for the confusion
0
Éric MoreauSenior .Net ConsultantCommented:
So now, if mcrider is correct, you should accept his answer!
0
chillAuthor Commented:
Please forgive me guys. Mcrider is correct. I do not want to use Forms. Incorrectly I referred to the incorrect solution in my previous posting.

Sorry for the confusion
0
mcriderCommented:
Thanks for the points!  Glad I could help!


Cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.