下面是一个列表框的例子,给你参考一下,在文本框中输入匹配列表中的内容时,实时高亮。 其中search是个函数,用于搜索列表框控件中符合这个变元的项目。 private sub txt_change() position=search(trim$(txt.Text)) if position>=0 then list1.listindex=position else list1.listindex=-1 endif 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) As Long'组合框列表增量查找 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
其中search是个函数,用于搜索列表框控件中符合这个变元的项目。
private sub txt_change()
position=search(trim$(txt.Text))
if position>=0 then
list1.listindex=position
else
list1.listindex=-1
endif
end sub
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