Option Strict On
Option Explicit On
Imports System.Windows.Forms
Public Class AutoCompleteCombo
Inherits ComboBox
Private _numeric As Boolean
Private mResetOnClear As Boolean = False
Protected Overrides Sub RefreshItem(ByVal index As Integer)
MyBase.RefreshItem(index)
End Sub
Public Property ItemIndex() As Integer
Get
Return MyBase.SelectedIndex
End Get
Set(ByVal value As Integer)
MyBase.SelectedIndex = value
End Set
End Property
Protected Overrides Sub SetItemsCore(ByVal items As System.Collections.IList)
MyBase.SetItemsCore(items)
End Sub
Public Property Numeric() As Boolean
Get
Return _numeric
End Get
Set(ByVal value As Boolean)
_numeric = value
End Set
End Property
Dim _ro As Boolean
Public Property Read_Only() As Boolean
Get
Return _ro
End Get
Set(ByVal value As Boolean)
_ro = value
If value Then
MyBase.BackColor = SystemColors.Control
MyBase.Enabled = False
Else
MyBase.BackColor = SystemColors.Window
MyBase.Enabled = True
End If
End Set
End Property
Public Shadows Sub KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
Dim intIndex As Integer
Dim strEntry As String
If Numeric Then
If Not IsNumeric(e.KeyChar) Then
If MyBase.Text.Length > 0 Then
'MyBase.Text = MyBase.Text.Substring(1, MyBase.Text.Length - 1)
MyBase.SelectionStart = MyBase.Text.Length
End If
e.Handled = True
Exit Sub
End If
End If
If Char.IsControl(e.KeyChar) Then
If MyBase.SelectionStart <= 1 Then
If mResetOnClear Then
MyBase.SelectedIndex = 0
MyBase.SelectAll()
Else
MyBase.Text = String.Empty
MyBase.SelectedIndex = -1
End If
e.Handled = True
Exit Sub
End If
If MyBase.SelectionLength = 0 Then
strEntry = MyBase.Text.Substring(0, MyBase.Text.Length - 1)
Else
strEntry = MyBase.Text.Substring(0, MyBase.SelectionStart - 1)
End If
ElseIf (Not Char.IsLetterOrDigit(e.KeyChar)) And (Not Char.IsWhiteSpace(e.KeyChar)) Then '< 32 Or KeyAscii > 127 Then
Exit Sub
Else
If MyBase.SelectionLength = 0 Then
strEntry = UCase(MyBase.Text & e.KeyChar)
Else
strEntry = MyBase.Text.Substring(0, MyBase.SelectionStart) & e.KeyChar
End If
End If
intIndex = MyBase.FindString(strEntry)
If intIndex <> -1 Then
MyBase.SelectedIndex = intIndex
MyBase.SelectionStart = strEntry.Length
MyBase.SelectionLength = MyBase.Text.Length - MyBase.SelectionStart
Else
MyBase.SelectedIndex = -1
MyBase.Text = strEntry
MyBase.SelectionStart = strEntry.Length
End If
e.Handled = True
Exit Sub
End Sub
Public Property ResetOnClear() As Boolean
Get
Return mResetOnClear
End Get
Set(ByVal Value As Boolean)
mResetOnClear = Value
End Set
End Property
End Class