PublicClass AutoComboboxClass AutoCombobox Inherits System.Windows.Forms.ComboBox Windows 窗体设计器生成的代码#Region " Windows 窗体设计器生成的代码 " PublicSub New()SubNew() MyBase.New() '该调用是 Windows 窗体设计器所必需的。 InitializeComponent() '在 InitializeComponent() 调用之后添加任何初始化 End Sub 'UserControl 重写 dispose 以清理组件列表。 ProtectedOverloadsOverridesSub Dispose()Sub Dispose(ByVal disposing AsBoolean) If disposing Then IfNot (components IsNothing) Then components.Dispose() EndIf EndIf MyBase.Dispose(disposing) End Sub 'Windows 窗体设计器所必需的 Private components As System.ComponentModel.IContainer '注意: 以下过程是 Windows 窗体设计器所必需的 '可以使用 Windows 窗体设计器修改此过程。 '不要使用代码编辑器修改它。 <System.Diagnostics.DebuggerStepThrough()>PrivateSub InitializeComponent()Sub InitializeComponent() ' 'AutoCombobox ' Me.Name ="AutoCombobox" End Sub #End Region PrivateSub AutoComboBox_KeyPress()Sub AutoComboBox_KeyPress(ByVal sender AsObject, ByVal e As System.Windows.Forms.KeyPressEventArgs) HandlesMyBase.KeyPress AutoComplete(Me, e) End Sub PublicSub AutoComplete()Sub AutoComplete(ByRef cb As ComboBox, ByVal e As System.Windows.Forms.KeyPressEventArgs) Dim strFindStr AsString If e.KeyChar =Chr(8) Then If cb.SelectionStart <=1Then cb.Text ="" Exit Sub EndIf If cb.SelectionLength =0Then strFindStr = cb.Text.Substring(0, cb.Text.Length -1) Else strFindStr = cb.Text.Substring(0, cb.SelectionStart -1) EndIf Else If cb.SelectionLength =0Then strFindStr = cb.Text & e.KeyChar Else strFindStr = cb.Text.Substring(0, cb.SelectionStart) & e.KeyChar EndIf EndIf Dim intIdx AsInteger=-1 intIdx = cb.FindString(strFindStr) If intIdx <>-1Then cb.SelectedText ="" cb.SelectedIndex = intIdx cb.SelectionStart = strFindStr.Length cb.SelectionLength = cb.Text.Length e.Handled =True EndIf End Sub End Class