March 3, 2021
Hot Topics:

VB Coding Tip: Using the Auto-Complete Function

  • By Sam Huggill
  • Send Email »
  • More Articles »

Microsoft Internet Explorer now supports this cool feature - add it to your applications using this tip.

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_FINDSTRING = &H14C
Private Const CB_ERR = (-1)

Private Declare Function PostMessage _ 
   Lib "user32" Alias _
   "PostMessageA" (ByVal hwnd As Long, _ 
                   ByVal wMsg As Long, _
                   ByVal wParam As Long, _
                   ByVal lParam As Long) As Long

Private Const CB_SETCURSEL = &H14E
Private m_bEditFromCode As Boolean

Private Sub Form_Load()

With Combo1
  .AddItem "VB Square"
  .AddItem "http://www.vbsquare.com"
  .AddItem "VB World"
  .AddItem "http://www.vb-world.net"
End With

End Sub

Private Sub Combo1_Change()
Dim i As Long, j As Long
Dim strPartial As String, strTotal As String
If m_bEditFromCode Then
m_bEditFromCode = False
Exit Sub
End If

With Combo1

strPartial = .Text
i = SendMessage(.hwnd, CB_FINDSTRING, -1, _ 
                ByVal strPartial)

If i <> CB_ERR Then
  strTotal = .List(i)
  j = Len(strTotal) - Len(strPartial)
  If j <> 0 Then
    m_bEditFromCode = True
    .SelText = Right$(strTotal, j)
    'Select unmatched characters
    .SelStart = Len(strPartial)
    .SelLength = j
  End If
End If
End With

End Sub

Private Sub Combo1_KeyDown(KeyCode As Integer, _ 
                           Shift As Integer)

Select Case KeyCode
Case vbKeyDelete
  m_bEditFromCode = True
Case vbKeyBack
  m_bEditFromCode = True
End Select

End Sub

This article was originally published on November 19, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date