Add ‘Type Ahead’ Functionality to Combo Boxes

Add ‘Type Ahead’ Functionality to Combo Boxes

You can add ‘Type Ahead’ functionality to the standard VB ComboBoxcontrol with this subroutine:

 Sub ComboTypeAhead(Combo As ComboBox)Dim i As Integer, _        KeyString As String, _        KeyLen As IntegerKeyString =3D Combo.TextKeyLen =3D Len(KeyString)gblTypeAhead =3D FalseIf KeyLen > 0 ThenFor i =3D 0 To Combo.ListCount - 1If Left(Combo.List(i), KeyLen) =3D _        KeyString ThenCombo.SetFocusCombo.ListIndex =3D iCombo.SelStart =3D KeyLenCombo.SelLength =3D Len(Combo.Text) _        - KeyLenExit ForEnd IfNext iEnd IfEnd Sub

Call this routine from the Change event of the ComboBox with thename of the ComboBox as the only parameter. Additionally, youmay want to add some code to the KeyPress event to handle theDelete and Backspace keys (that is where the gblTypeAhead variableis used), but other than that, this routine will handle all ofthe type-ahead functionality.


Share the Post: