Microsoft & .NETVisual BasicVB Coding Tip: Using the Auto-Complete Function

VB Coding Tip: Using the Auto-Complete Function

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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
  Else
  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

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories