Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combo box lookahead

Status
Not open for further replies.

vbjock

Programmer
Jul 13, 2007
16
US
I have a combo box created using VB6 with the following items:
Adams
Arlington
Barnes
Bielaski
Carmichael
Iverson
Izrak

When I type B on the combo box, Barnes is shown because Style = 2-drop down list.
When I type I, Iverson shows up. How can I make the combo box highlight Bielaski after typing I?
Thanks
 
Er ... I get (and expect) the behaviour you are asking for, rather than the behaviour you say you are getting (Note that it depends on the amount of time between keypresses; wait too long after pressing B and the combo search resets itself, treating the next letter - I - as if it is the first letter typed ...)
 
I believe you can not have what you want with VB’s drop down list. If you want to select Bielaski you need to press B twice.

My co-worker many years ago tried to make combo box (regular drop down combo) to work the way you want, with KeyPress, KeyDown, KeyUp, GetFocus, LostFocus etc. events, accommodating A-Z, a-z, 0-9 and so forth, but it never worked quiet right.


Have fun.

---- Andy
 
Sorry - forgot to say that Sorted needs to be set to true for it to work
 
Try calling this from your ComboKeyPress event;

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Sub AccessComboKeyPress(Combo As ComboBox, KeyAscii As Integer)

Dim lret As Long
Dim FindString As String
'simulates MSAccess style searching combo
' this version is not case sensitive

Const CB_FINDSTRING = &H14C

With Combo
If .SelLength = 0 Then
FindString = .Text & Chr$(KeyAscii)
Else
FindString = Left$(.Text, .SelStart) & Chr$(KeyAscii)
End If
lret = SendMessage(.hwnd, CB_FINDSTRING, -1, ByVal FindString$)
If lret >= 0 Then
.ListIndex = lret
.SelStart = Len(FindString)
.SelLength = Len(.Text) - .SelStart
KeyAscii = 0
End If
End With

End Sub
 
You will probably be needing this too!

Call it from the LostFocus event of the Combo

Sub AccessComboLostFocus(Combo As ComboBox)

If Combo.ListIndex = -1 Then
DoEvents
MsgBox "The text you entered is not an item in the list" & vbCrLf & _
"Select an item from the list, or enter text which" & vbCrLf & _
" matches one of the listed items", vbExclamation, Combo.Tag

Combo.SetFocus
DoEvents
Sendkeys "{F4}", True
Combo.ListIndex = -1
End If

End Sub
 
A very long time since I reviewed the code in my Sub AccessComboLostFocus; the DoEvents can/ should be cut.
 
>because Style = 2-drop down list

Oh and you will require to set Style to 0-Dropdown Combo, for my suggestions to work.
 
Dear Colleagues:
Thanks for all your help. I tried using on Keypress and also using a Search button but the suggestion of HughLerwill worked quite well. I am indebted to Tek-tips and I just sent a modest contribution.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top