Link to home
Start Free TrialLog in
Avatar of chill
chill

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of mcrider
mcrider

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chill
chill

ASKER

Although this is a solution I would rather not use FM20. Any other solutions?
Avatar of Éric Moreau
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!
Avatar of chill

ASKER

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.
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!
Avatar of chill

ASKER

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.
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!
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.
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!
mcrider,

the guy doesn't want to use Forms!
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!
Avatar of chill

ASKER

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
So now, if mcrider is correct, you should accept his answer!
Avatar of chill

ASKER

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
Thanks for the points!  Glad I could help!


Cheers!