6 楼的只能定位第一个字符。楼主说的那叫增量查找,下面是一个例子,但只支持西文,不支持中文:Public Sub ComboIncrementalSearch(cbo As _ ComboBox, KeyAscii As Integer) Static dTimerLast As Double Static sSearch As String Static hWndLast As Long Dim nRet As Long Const MAX_KEYPRESS_TIME = 0.5 ' Weed out characters that are not scanned If (KeyAscii < 32 Or KeyAscii > 127) _ Then Exit Sub If (Timer - dTimerLast) < _ MAX_KEYPRESS_TIME And hWndLast = _ cbo.hWnd Then sSearch = sSearch & Chr$(KeyAscii) Else sSearch = Chr$(KeyAscii) hWndLast = cbo.hWnd End If ' Search the combo box nRet = SendMessage(cbo.hWnd, _ CB_FINDSTRING, -1, ByVal sSearch) If nRet >= 0 Then cbo.ListIndex = nRet End If KeyAscii = 0 dTimerLast = Timer End Sub
Private Sub Combo1_Change() On Error Resume Next Controls(Combo1.Name & "_List").Text = Combo1.Text If Controls(Combo1.Name & "_List").Text = Combo1.Text Then MsgBox "OK" End If End SubPrivate Sub Form_Load() Combo1.AddItem "A001" Combo1.AddItem "A002" Combo1.AddItem "A003" Combo1.AddItem "C003" Combo1.AddItem "E008" Dim i As Integer Controls.Add "VB.ListBox", Combo1.Name & "_List" For i = 0 To Combo1.ListCount - 1 Controls(Combo1.Name & "_List").AddItem Combo1.List(i) Next End Sub
Private Declare Function SendMessage& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) Private Const CB_FINDSTRING = &H14CPrivate Sub Combo1_Change() Dim ret As Long ret = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal Combo1.Text) If ret >= 0 Then If Combo1.List(ret) = Combo1.Text Then MsgBox "ListIndex -> " & ret End If End If End SubPrivate Sub Form_Load() Combo1.AddItem "A001" Combo1.AddItem "A002" Combo1.AddItem "A003" Combo1.AddItem "C003" Combo1.AddItem "E008" End Sub
Private Declare Function SendMessage& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) Private Const CB_FINDSTRING = &H14C Private Const CB_FINDSTRINGEXACT = &H158Private Sub Combo1_Change() Dim ret As Long ret = SendMessage(Combo1.hwnd, CB_FINDSTRINGEXACT, -1, ByVal Combo1.Text) If ret >= 0 Then MsgBox "ListIndex -> " & ret End If End SubPrivate Sub Form_Load() Combo1.AddItem "A001" Combo1.AddItem "A002" Combo1.AddItem "A003" Combo1.AddItem "C003" Combo1.AddItem "E008" End Sub
Private Sub Combo1_LostFocus()
Combo1.AddItem Combo1.Text
End Sub
On Error GoTo ErrLine
For I = 0 To comTmp.ListCount - 1
If Trim(comTmp.List(I)) = strText Then
comTmp.ListIndex = I: Exit For
End If
Next I
也可以在change事件調用,也可以在失去焦點時調用
2:
Combo1.AddItem "1111"
Combo1.AddItem "2222"
Combo1.AddItem "3333"
Combo1.AddItem "4444"
Combo1.AddItem "5555"
Combo1.AddItem "6666"
3:在COMBO上输入1,2,3等,自动定位到1111,2222,3333
6 楼的只能定位第一个字符。楼主说的那叫增量查找,下面是一个例子,但只支持西文,不支持中文:Public Sub ComboIncrementalSearch(cbo As _
ComboBox, KeyAscii As Integer)
Static dTimerLast As Double
Static sSearch As String
Static hWndLast As Long
Dim nRet As Long
Const MAX_KEYPRESS_TIME = 0.5
' Weed out characters that are not scanned
If (KeyAscii < 32 Or KeyAscii > 127) _
Then Exit Sub
If (Timer - dTimerLast) < _
MAX_KEYPRESS_TIME And hWndLast = _
cbo.hWnd Then
sSearch = sSearch & Chr$(KeyAscii)
Else
sSearch = Chr$(KeyAscii)
hWndLast = cbo.hWnd
End If
' Search the combo box
nRet = SendMessage(cbo.hWnd, _
CB_FINDSTRING, -1, ByVal sSearch)
If nRet >= 0 Then
cbo.ListIndex = nRet
End If
KeyAscii = 0
dTimerLast = Timer
End Sub
Private Sub Combo1_Change()
On Error Resume Next
Controls(Combo1.Name & "_List").Text = Combo1.Text
If Controls(Combo1.Name & "_List").Text = Combo1.Text Then
MsgBox "OK"
End If
End SubPrivate Sub Form_Load()
Combo1.AddItem "A001"
Combo1.AddItem "A002"
Combo1.AddItem "A003"
Combo1.AddItem "C003"
Combo1.AddItem "E008"
Dim i As Integer
Controls.Add "VB.ListBox", Combo1.Name & "_List"
For i = 0 To Combo1.ListCount - 1
Controls(Combo1.Name & "_List").AddItem Combo1.List(i)
Next
End Sub
Private Const CB_FINDSTRING = &H14CPrivate Sub Combo1_Change()
Dim ret As Long
ret = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal Combo1.Text)
If ret >= 0 Then
If Combo1.List(ret) = Combo1.Text Then
MsgBox "ListIndex -> " & ret
End If
End If
End SubPrivate Sub Form_Load()
Combo1.AddItem "A001"
Combo1.AddItem "A002"
Combo1.AddItem "A003"
Combo1.AddItem "C003"
Combo1.AddItem "E008"
End Sub
Private Const CB_FINDSTRING = &H14C
Private Const CB_FINDSTRINGEXACT = &H158Private Sub Combo1_Change()
Dim ret As Long
ret = SendMessage(Combo1.hwnd, CB_FINDSTRINGEXACT, -1, ByVal Combo1.Text)
If ret >= 0 Then
MsgBox "ListIndex -> " & ret
End If
End SubPrivate Sub Form_Load()
Combo1.AddItem "A001"
Combo1.AddItem "A002"
Combo1.AddItem "A003"
Combo1.AddItem "C003"
Combo1.AddItem "E008"
End Sub