(转)使用一个下拉框或文本框+列表框的方式实现类似IE地址栏自动完成的功能

转自"问专家:QA003240"

可以使用一个下拉框或文本框+列表框的方式实现。这样做的好处是下拉框的内容不一定是URL,可以是任意内容。下面是一个用下拉框实现的示例:
    ' *************Declarations
    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 Const WM_SETREDRAW As Long = &HB&
    Private Const CB_FINDSTRING As Long = &H14C&
    
    *************Here is the sub that will implement auto search
    
    Public Sub SearchCombo(InControl As Object)
    On Error GoTo trap
    
    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
    trap:
     MsgBox Err.Description
    End Sub
    
    '**************Implement it like this************
    Private Sub cboParent_Change()
     Call SearchCombo(cboParent) '<--pass the combo box to the sub by name
    End Sub 
    
此问题由vbfan回答。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值