模仿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

posted on 2004-11-17 12:20  虫子  阅读(1143)  评论(0编辑  收藏  举报

导航