模仿IE自动完成的ComboBox

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const CB_SHOWDROPDOWN = &H14F
Private Const WM_SETREDRAW As Long = &HB&
Private Const CB_FINDSTRING As Long = &H14C&

'iKeyCode目的:传递KeyDown事件中的KeyCode
Private iKeyCode As Integer

Public Sub SearchCombo(InControl As Object)
    On Error GoTo errTrap
    
    Dim StrPos As Long
    Dim lPos As Long
    Dim SearchStr As String
    
    If TypeOf InControl Is ComboBox Then
        StrPos = InControl.SelStart
        SearchStr = Left$(InControl.Text, StrPos)
       
        lPos = SendMessage(InControl.hwnd, CB_FINDSTRING, 0, ByVal SearchStr)
       
        If lPos >= 0 Then
            InControl.Text = InControl.List(lPos)
            InControl.ListIndex = lPos
        End If
       
        With InControl
            .SelStart = StrPos
            .SelLength = Len(InControl.Text)
        End With
    End If
    Exit Sub
errTrap:
     MsgBox Err.Description
End Sub
 

Private Sub Combo1_Change()
    If iKeyCode <> vbKeyBack And iKeyCode <> vbKeyReturn Then
        SearchCombo Combo1
        SendMessageLong Combo1.hwnd, CB_SHOWDROPDOWN, True, 0
    End If
End Sub

Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
    iKeyCode = KeyCode
End Sub

Private Sub Form_Load()
    Combo1.AddItem "asqwed"
    Combo1.AddItem "fgtthgh"
    Combo1.AddItem "nfgftrtyr"
    Combo1.AddItem "werwerwe"
    Combo1.AddItem "ytrtyrtyrty"
    Combo1.AddItem "bfrthrtyrt"
    Combo1.AddItem "rewerwe"
    Combo1.AddItem "vsfsdf"
End Sub

转载于:https://www.cnblogs.com/lxcc/archive/2004/11/17/64717.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值