查看: 651  |  回复: 0
  VB6 ComboBox控件的自动查询
楼主
发表于 2023年4月18日 15:57

新建From1,新建Combo1(按钮CommandButton),List1(ListBox),代码:

Private Sub Combo1_Change()
    Dim sString As String
    Dim start As Integer
    start = Combo1.SelStart
    sString = Left(Combo1.Text, start)
    For i = 0 To Combo1.ListCount - 1 Step 1
        Dim sitem As String
        sitem = Combo1.List(i)
        sitem = Left(sitem, start)
        If sitem = sString Then
            List1.ListIndex = i
            List1.Visible = True
            Exit For
        End If
    Next
End Sub

Private Sub Combo1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        Combo1.ListIndex = List1.ListIndex
        List1.Visible = False
    End If
End Sub

Private Sub Form_Load()
    Combo1.AddItem "abc"
    Combo1.AddItem "acb"
    Combo1.AddItem "edf"
    Combo1.AddItem "ffff"
    Combo1.AddItem "manongku"
    '向Combo1添加列表项
    
    Dim i As Integer
    For i = 0 To Combo1.ListCount - 1 Step 1
        List1.AddItem Combo1.List(i), i
    Next
    List1.Visible = False
    '向List1添加与Combo1相同的列表项
End Sub

输入m可以看到自动选择了manongku。

您需要登录后才可以回帖 登录 | 立即注册
【本版规则】请勿发表违反国家法律的内容,否则会被冻结账号和删贴。
用户名: 立即注册
密码:
2020-2025 MaNongKu.com