其实这类的文章在国外的技术网站上也有很多,但是都没有实现combobox的自动下拉.
在这里实现了自动下拉.
在这里我把combobox做成了一个控件(XComBo)来实现功能.它从ComboBox,可以将新建的
工程的继承改为 Inherits System.Windows.Forms.ComboBox
我认为很关键的两点是API函数SendMessage的调用和KeyPress代码段e.Handled = True的应用。
If the event is not handled, it will be sent to the operating system for default processing. Set Handled to true to cancel the KeyPress event.
Control's code:(100%原创)
Public
Class XComBo
Class XComBo
Inherits System.Windows.Forms.ComboBox
#Region " Windows Form Designer generated code "
Public Sub New()Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'UserControl1 overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
Private Declare Function SendMessage()Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const CB_SHOWDROPDOWN = &H14F
Private Sub ComboDropdown()Sub ComboDropdown()
SendMessage(Handle.ToInt32, CB_SHOWDROPDOWN, 1, 0&)
End Sub
Protected Overrides Sub OnKeyPress()Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim str, strCurrentText As String
If ((Asc(e.KeyChar) = 13) And (Asc(e.KeyChar) = 8)) Then Exit Sub
ComboDropdown()
strCurrentText = Mid(Text, 1, Text.Length - SelectedText.Length) & CStr(e.KeyChar)
str = CStr(strCurrentText)
Dim Index As Integer = FindString(str)
If Index >= 0 Then
SelectedIndex = Index
[Select](str.Length, Items(Index).length - str.Length)
e.Handled = True
End If
End Sub
End Class
Inherits System.Windows.Forms.ComboBox
#Region " Windows Form Designer generated code "
Public Sub New()Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'UserControl1 overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
Private Declare Function SendMessage()Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const CB_SHOWDROPDOWN = &H14F
Private Sub ComboDropdown()Sub ComboDropdown()
SendMessage(Handle.ToInt32, CB_SHOWDROPDOWN, 1, 0&)
End Sub
Protected Overrides Sub OnKeyPress()Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim str, strCurrentText As String
If ((Asc(e.KeyChar) = 13) And (Asc(e.KeyChar) = 8)) Then Exit Sub
ComboDropdown()
strCurrentText = Mid(Text, 1, Text.Length - SelectedText.Length) & CStr(e.KeyChar)
str = CStr(strCurrentText)
Dim Index As Integer = FindString(str)
If Index >= 0 Then
SelectedIndex = Index
[Select](str.Length, Items(Index).length - str.Length)
e.Handled = True
End If
End Sub
End Class