ComboAtSearch




Option Explicit
Dim IgnoreTextChange As Boolean
Private Sub Combo1_Change()
Dim i%
Dim NewText$
' Check to see if a serch is required.

If Not IgnoreTextChange And Combo1.ListCount > 0 Then
' Loop through the list searching for a partial match of

' the entered text.

For i = 0 To Combo1.ListCount - 1
NewText = Combo1.List(i)
If InStr(1, NewText, Combo1.Text, 1) = 1 Then
If Len(Combo1.Text) <> Len(NewText) Then
' Partial match found

' Avoid recursively entering this event

IgnoreTextChange = True
i = Len(Combo1.Text)
' Attach the full text from the list to what has

' already been entered. This technique preserves

' the case entered by the user.

Combo1.Text = Combo1.Text & Mid$(NewText, i + 1)
' Select the text that is auto-entered

Combo1.SelStart = i
Combo1.SelLength = Len(Mid$(NewText, i + 1))
Exit For
End If
End If
Next
Else
' The IgnoreTwextChange Flag is only effective for one

' Changed event.

IgnoreTextChange = False
End If
End Sub

Private Sub Combo1_GotFocus()
' Select existing text on entry to the combo box

Combo1.SelStart = 0
Combo1.SelLength = Len(Combo1.Text)
End Sub

Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
' If a user presses the "Delete" key, then the selected text

' is removed.

If KeyCode = vbKeyDelete And Combo1.SelText <> "" Then
' Make sure that the text is not automatically re-entered

' as soon as it is deleted

IgnoreTextChange = True
Combo1.SelText = ""
KeyCode = 0
End If
End Sub

Private Sub Combo1_KeyPress(KeyAscii As Integer)
' If a user presses the "Backspace" key, then the selected text

' is removed. Autosearch is not re-performed, as that would only

' put it straight back again.

If KeyAscii = 8 Then
IgnoreTextChange = True
If Len(Combo1.SelText) Then
Combo1.SelText = ""
KeyAscii = 0
End If
End If
End Sub

Inserite una ComboBox nel progetto
Nominatela Combo1, Settate la proprieta style su :

0 - Dropdown Combo
oppure
1 - Simple Combo.










( comboatsearch.html )- by Paolo Puglisi - Modifica del 17/12/2023