Advertisement
5_2007-2008 VB function enhancement #177616

Auto Complete Combo

It's sole purpose is to autocomplete-enable a combo box with as few coding as possible. Uses windows messaging sub-system to send directly a message to to the window's windows procedure. It's not mine but I thought it was great and wanted to share it with you. So no voting people. Cheers :)

AI

AI Samenvatting: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Broncode
original-source
Option Explicit
Private Sub Form_Load()
With Combo1
  .AddItem "ABCD"
  .AddItem "ACDE"
  .AddItem "ADEF"
  .AddItem "AEFG"
  .AddItem "ACFG"
  .AddItem "AFGH"
  .AddItem "AGHI"
 End With
End Sub
Private Sub Combo1_KeyPress(KeyAscii As Integer)
Dim i As Long
Dim iNewStart As Integer
Dim strTemp As String
'Figure out the string prefix to search gor
  If Combo1.SelStart = 0 Then
    strTemp = Combo1.Text & Chr(KeyAscii)
  Else
    strTemp = Left(Combo1.Text, Combo1.SelStart) & Chr(KeyAscii)
  End If
'Pass -1 as lParam to search entire list
  i = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, strTemp)
'-1 return code indicates failure to find the string  
  If i <> -1 Then
    'SendMessage returns the index of the first occurrence
    'of strTemp in the combo's list.
    Combo1.Text = Combo1.List(i)
    'Set the text selection appropriately for the
    'suggested match
    Combo1.SelStart = Len(strTemp)
    Combo1.SelLength = Len(Combo1.List(i)) - Len(strTemp)
    KeyAscii = 0
  End If
  
End Sub
Originele reacties (3)
Hersteld van de Wayback Machine