这是找到的能够实现类似功能的组合框的代码,改动一下能够实现。
' Enable extended matching to any type combobox control
'
' Extended matching means that as soon as you type in the edit area
' of the ComboBox control, the routine searches for a partial match
' in the list area and highlights the characters left to be typed.
'
' To enable this capability you have only to call this routine
' from within the KeyPress routine of the ComboBox, as follows:
'
' Private Sub Combo1_KeyPress(KeyAscii As Integer)
' ComboBoxExtendedMatching Combo1, KeyAscii
' End SubSub ComboBoxExtendedMatching(cbo As ComboBox, KeyAscii As Integer, _
Optional CompareMode As VbCompareMethod = vbTextCompare)
Dim index As Long
Dim Text As String
' if user pressed a control key, do nothing
If KeyAscii <= 32 Then Exit Sub
' produce new text, cancel automatic key processing
Text = Left$(cbo.Text, cbo.SelStart) & Chr$(KeyAscii) & Mid$(cbo.Text, _
cbo.SelStart + 1 + cbo.SelLength)
KeyAscii = 0
' search the current item in the list
For index = 0 To cbo.ListCount - 1
If InStr(1, cbo.List(index), Text, CompareMode) = 1 Then
' we've found a match
cbo.ListIndex = index
Exit For
End If
Next
' if no matching item
If index = cbo.ListCount Then
cbo.Text = Text
End If
' highlight trailing chars in the edit area
cbo.SelStart = Len(Text)
cbo.SelLength = 9999
End Sub
' Enable extended matching to any type combobox control
'
' Extended matching means that as soon as you type in the edit area
' of the ComboBox control, the routine searches for a partial match
' in the list area and highlights the characters left to be typed.
'
' To enable this capability you have only to call this routine
' from within the KeyPress routine of the ComboBox, as follows:
'
' Private Sub Combo1_KeyPress(KeyAscii As Integer)
' ComboBoxExtendedMatching Combo1, KeyAscii
' End SubSub ComboBoxExtendedMatching(cbo As ComboBox, KeyAscii As Integer, _
Optional CompareMode As VbCompareMethod = vbTextCompare)
Dim index As Long
Dim Text As String
' if user pressed a control key, do nothing
If KeyAscii <= 32 Then Exit Sub
' produce new text, cancel automatic key processing
Text = Left$(cbo.Text, cbo.SelStart) & Chr$(KeyAscii) & Mid$(cbo.Text, _
cbo.SelStart + 1 + cbo.SelLength)
KeyAscii = 0
' search the current item in the list
For index = 0 To cbo.ListCount - 1
If InStr(1, cbo.List(index), Text, CompareMode) = 1 Then
' we've found a match
cbo.ListIndex = index
Exit For
End If
Next
' if no matching item
If index = cbo.ListCount Then
cbo.Text = Text
End If
' highlight trailing chars in the edit area
cbo.SelStart = Len(Text)
cbo.SelLength = 9999
End Sub
解决方案 »
- 我是做Delphi開發的。來問個VBA的問題,怎麼把VB代碼放到Office 的VBA里執行?里面VB的代碼和需求!
- 滚动条的问题,困惑我很长时间了!
- 数据筛选难题,请大家进来看看,谢谢!!!结贴给分
- 连接中的USER和PASSWORD可以用变量U1和P1来代替吗?应该是如何写的?
- 如何列出一个对象的所有事件及其参数?我好像找到了列第三方控件事件的办法,可是对VB自带的控件没有用。
- 请问FONTSIZE=16的字它的高和宽是多少,有公式吗?
- ACCESS数据库能否存贮WEB页?
- 我用VB写了一个COM,已经用SignCode作过数字签名,可是在网页中仍然显示“控件不安全”?
- VB怎样控制终止某过程
- 怎样动态连接多个数据库?
- smtp
- DataGrid问题 在线!
hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As LongConst LB_FINDSTRING = &H18F
Const LB_FINDSTRINGEXACT = &H1A2
Const CB_FINDSTRING = &H14C
Const CB_FINDSTRINGEXACT = &H158' 在combobox或者listbox中查找匹配项
' 返回索引号,没有找到返回-1Function ListBoxFindString(ctrl As Control, ByVal search As String, _
Optional startIndex As Long = -1, Optional ExactMatch As Boolean) As Long
Dim uMsg As Long
If TypeOf ctrl Is ListBox Then
uMsg = IIf(ExactMatch, LB_FINDSTRINGEXACT, LB_FINDSTRING)
ElseIf TypeOf ctrl Is ComboBox Then
uMsg = IIf(ExactMatch, CB_FINDSTRINGEXACT, CB_FINDSTRING)
Else
Exit Function
End If
ListBoxFindString = SendMessage(ctrl.hWnd, uMsg, startIndex, ByVal search)
End Function