McMillan's Visual Basic Code - Code Snippets - Auto-selecting items in a Combo Box
Code Snippets
Home



Auto-selecting items in a Combo Box

This snippet shows you how to auto-select a combox as the user types

Option Explicit

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

Private Const CB_ERR = -1
Private Const CB_FINDSTRING = &H14C

Private Sub Combo1_KeyPress(KeyAscii As Integer)

   Dim r As Long
   Dim s As String
   Dim l As Long

   s = Left$(Combo1.Text, Combo1.SelStart) & Chr$(KeyAscii)
   r = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal s)
   If r <> CB_ERR Then
      With Combo1
         .ListIndex = r
         .SelStart = Len(s)
         .SelLength = Len(.Text) - Len(s)
      End With
      KeyAscii = 0
   End If

End Sub

© Copyright Notice

Unless otherwise stated, the code on this site is Copyright to Andrew McMillan. You may use this code in your projects (both commercial and non-commercial) but you are not permitted to republish this code in any form without the Author's prior consent.

The code on this site is supplied "as is" and no claims are made as to its soundness. The Author claims no responsibility for or liability from use of said source code.


Home