Option ExplicitPrivate Sub Combo1_Change() '自动搜索选项 Dim i As Integer If Trim(Combo1.Text) = "" Then Exit Sub For i = 0 To 10 If InStr(UCase(Combo1.List(i)), UCase(Combo1.Text)) > 0 Then Combo1.ListIndex = i Exit For End If Next End SubPrivate Sub Command1_Click() Text1.Text = Combo1.ListIndex End SubPrivate Sub Form_Load() Dim i As Integer Combo1.Clear For i = 0 To 10 Combo1.AddItem i & "测试" NextEnd Sub
Private Sub Combo1_Click() Text1.Text = Combo1.ListIndex End Sub加上这段后,会发现,当在Combo1中输入2时,text1也会显示2,但是如果点击按钮会发现还是-1,真不知道是怎么回事?难道是BUG
Combo1_KeyDown-1 Combo1_KeyPress-1 Combo1_Click 3 Combo1_Change 3 Combo1_KeyUp-1 执行到Combo1_KeyUp事件的时候又变成-1了如果不是靠按键触发change事件就没问题: Private Sub Command2_Click() Combo1.Text = "3" '这样就没问题 End Sub 不使用change事件,换成这样就能避开这个问题 Private Sub Combo1_KeyUp() '自动搜索选项 Dim i As Integer If Trim(Combo1.Text) = "" Then Exit Sub For i = 0 To 10 If InStr(UCase(Combo1.List(i)), UCase(Combo1.Text)) > 0 Then Combo1.ListIndex = i Exit For End If Next End Sub
设计时令 Combo1 的 Style 属性 = 2 - Dropdown List
就是为了有个搜索的功能,不能这么设置的!==================================你试过吗?下拉列表是可以设置的。任何事情不要想当然。惊叹号可以去掉,扎眼。另外,Style = 0 的情况下,除非通过点击选定列表项,否则,在使用 Set 方法的过程之外,Get 方法得到的 ListIndex 就是 -1。算了,教你一手。也许你的实际代码更复杂,有其他问题。Private Declare Function SendMessagebyString Lib _ "user32" Alias "SendMessageA" (ByVal hWND As Long, _ ByVal wMsg As Long, ByVal wParam As Long, _ ByVal lParam As String) As LongPrivate Const CB_FINDSTRING = &H14CDim n As Long Private Sub Combo1_Change() '自动搜索选项 Dim i As Integer If Trim(Combo1.Text) = "" Then Exit Sub n = SendMessagebyString(Combo1.hWND, CB_FINDSTRING, -1, Combo1.Text) If n >= 0 Then Combo1.ListIndex = n End SubPrivate Sub Command1_Click() Text1.Text = n Debug.Print Combo1.ListIndex End Sub
Option ExplicitPrivate Sub Combo1_Change()
'自动搜索选项
Dim i As Integer
If Trim(Combo1.Text) = "" Then Exit Sub
For i = 0 To 10
If InStr(UCase(Combo1.List(i)), UCase(Combo1.Text)) > 0 Then
Combo1.ListIndex = i
Exit For
End If
Next
End SubPrivate Sub Command1_Click()
Text1.Text = Combo1.ListIndex
End SubPrivate Sub Form_Load()
Dim i As Integer
Combo1.Clear For i = 0 To 10
Combo1.AddItem i & "测试"
NextEnd Sub
的值,居然还是-1!为什么?
Private Sub Combo1_Click()
Text1.Text = Combo1.ListIndex
End Sub加上这段后,会发现,当在Combo1中输入2时,text1也会显示2,但是如果点击按钮会发现还是-1,真不知道是怎么回事?难道是BUG
Combo1_KeyPress-1
Combo1_Click 3
Combo1_Change 3
Combo1_KeyUp-1
执行到Combo1_KeyUp事件的时候又变成-1了如果不是靠按键触发change事件就没问题:
Private Sub Command2_Click()
Combo1.Text = "3" '这样就没问题
End Sub
不使用change事件,换成这样就能避开这个问题
Private Sub Combo1_KeyUp()
'自动搜索选项
Dim i As Integer
If Trim(Combo1.Text) = "" Then Exit Sub
For i = 0 To 10
If InStr(UCase(Combo1.List(i)), UCase(Combo1.Text)) > 0 Then
Combo1.ListIndex = i
Exit For
End If
Next
End Sub
设计时令 Combo1 的 Style 属性 = 2 - Dropdown List
就是为了有个搜索的功能,不能这么设置的!==================================你试过吗?下拉列表是可以设置的。任何事情不要想当然。惊叹号可以去掉,扎眼。另外,Style = 0 的情况下,除非通过点击选定列表项,否则,在使用 Set 方法的过程之外,Get 方法得到的 ListIndex 就是 -1。算了,教你一手。也许你的实际代码更复杂,有其他问题。Private Declare Function SendMessagebyString Lib _
"user32" Alias "SendMessageA" (ByVal hWND As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As String) As LongPrivate Const CB_FINDSTRING = &H14CDim n As Long
Private Sub Combo1_Change()
'自动搜索选项
Dim i As Integer
If Trim(Combo1.Text) = "" Then Exit Sub
n = SendMessagebyString(Combo1.hWND, CB_FINDSTRING, -1, Combo1.Text)
If n >= 0 Then Combo1.ListIndex = n
End SubPrivate Sub Command1_Click()
Text1.Text = n
Debug.Print Combo1.ListIndex
End Sub
不过设计时令 Combo1 的 Style 属性 = 2 - Dropdown List,如果设置成这个确实没有办法输入字符实现模糊查询啊!(输入数字,字母可能可以,但是中文呢?)
至于你的判断条件,由于技术有限,我没看明白