Auto Complete Combo Box
Thanks in advance,
Paul Tyng
Thanks in advance,
Paul Tyng
Public Class clsComboBox
Inherits ComboBox
Public Sub New()
AddHandler Me.KeyPress, AddressOf HandleKeyPress
End Sub
Private Sub HandleKeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim lngItemNum As Long
Dim lngSelectedLength As Long
Dim lngMatchLength As Long
Dim strCurrentText As String
Dim strSearchText As String
Dim sTypedText As String
Const CB_LOCKED = &H255
Try
With (Me)
If .Items.Count > 0 Then
If .Text = Nothing Then
.Tag = Nothing
Exit Sub
End If
If .Text.Length = 1 Then
.Tag = Nothing
End If
Me.BeginUpdate()
If ((InStr(1, .Text, .Tag, vbTextCompare) <> 0 And Len(.Tag) = Len(.Text) - 1) Or (InStr(.Text, 1) <> InStr(.Tag, 1) And .Tag <> Nothing)) And .Tag <> CStr(CB_LOCKED) Then
strSearchText = .Text
lngSelectedLength = Len(strSearchText)
lngItemNum = .FindString(strSearchText)
If Not (lngItemNum = -1) Then
.Tag = CB_LOCKED
.SelectedIndex = lngItemNum
lngMatchLength = Len(.Text) - lngSelectedLength
sTypedText = strSearchText
.SelectionStart = lngSelectedLength
Dim Temp As Integer
Temp = lngMatchLength
.SelectionLength = Temp
.Tag = sTypedText
End If
ElseIf .Tag <> CStr(CB_LOCKED) Then
.Tag = .Text
End If
Me.EndUpdate()
End If
End With
Catch err As Exception
MsgBox(err.Message & err.StackTrace)
End Try
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = 8 Then
Me.SelectedIndex = -1
Me.SelectedValue = ""
Me.Text = ""
End If
End Sub
End Class