100% .NET Control_自动完成Combobox的XComBo控件(VB.NET)

其实这类的文章在国外的技术网站上也有很多,但是都没有实现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%原创)

ExpandedBlockStart.gif ContractedBlock.gif    Public   Class XComBo Class XComBo
InBlock.gif    
Inherits System.Windows.Forms.ComboBox
InBlock.gif
InBlock.gif#Region 
" Windows Form Designer generated code "
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public Sub New()Sub New()
InBlock.gif        
MyBase.New()
InBlock.gif
InBlock.gif        
'This call is required by the Windows Form Designer.
InBlock.gif
        InitializeComponent()
InBlock.gif
InBlock.gif        
'Add any initialization after the InitializeComponent() call
InBlock.gif

ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif    
'UserControl1 overrides dispose to clean up the component list.
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
InBlock.gif        
If disposing Then
InBlock.gif            
If Not (components Is NothingThen
InBlock.gif                components.Dispose()
InBlock.gif            
End If
InBlock.gif        
End If
InBlock.gif        
MyBase.Dispose(disposing)
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif    
'Required by the Windows Form Designer
InBlock.gif
    Private components As System.ComponentModel.IContainer
InBlock.gif
InBlock.gif    
'NOTE: The following procedure is required by the Windows Form Designer
InBlock.gif
    'It can be modified using the Windows Form Designer.  
InBlock.gif
    'Do not modify it using the code editor.
ExpandedSubBlockStart.gifContractedSubBlock.gif
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
InBlock.gif        components 
= New System.ComponentModel.Container
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif#
End Region
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Declare Function SendMessage()Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As IntegerByVal wMsg As IntegerByVal wParam As IntegerByVal lParam As IntegerAs Integer
InBlock.gif    
Private Const CB_SHOWDROPDOWN = &H14F
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub ComboDropdown()Sub ComboDropdown()
InBlock.gif        SendMessage(Handle.ToInt32, CB_SHOWDROPDOWN, 
10&)
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Protected Overrides Sub OnKeyPress()Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
InBlock.gif
InBlock.gif        
Dim str, strCurrentText As String
InBlock.gif
InBlock.gif        
If ((Asc(e.KeyChar) = 13And (Asc(e.KeyChar) = 8)) Then Exit Sub
InBlock.gif
InBlock.gif        ComboDropdown()
InBlock.gif        strCurrentText 
= Mid(Text, 1, Text.Length - SelectedText.Length) & CStr(e.KeyChar)
InBlock.gif        
str = CStr(strCurrentText)
InBlock.gif        
Dim Index As Integer = FindString(str)
InBlock.gif        
If Index >= 0 Then
InBlock.gif            SelectedIndex 
= Index
InBlock.gif            [
Select](str.Length, Items(Index).length - str.Length)
InBlock.gif            e.Handled 
= True
InBlock.gif        
End If
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedBlockEnd.gif
End Class

None.gif
None.gif




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值