'****************************************************************************
'模块名称:mListBoxComboBoxSearch.bas
'发布日期:2009/03/06
'描 述:VB6在ListBox或ComBox中搜索字符串项的模块(支持模糊与精确查找)
'博 客:http://blog.csdn.net/tanaya
'e-mail :vbcoder@126.com
'****************************************************************************
Option Explicit
Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" ( _
ByVal hWnd As Long,_
ByVal wMsg As Long,_
ByVal wParam As Integer,_
ByVal lParam As Any) As Long
Private Declare Function SendMessageByString Lib "USER32" Alias "SendMessageA" ( _
ByVal hWnd As Long,_
ByVal wParam As Long,_
ByVal lParam As String) As Long
Private Const LB_FINDSTRINGEXACT = &H1A2 '在ListBox中精确查找
Private Const LB_FINDSTRING = &H18F '在ListBox中模糊查找
Private Const CB_FINDSTRINGEXACT = &H158 '在ComboBox中精确查找
Private Const CB_FINDSTRING = &H14C '在ComboBox中模糊查找
'其实返回值都是-1
Private Const LB_ERR = -1
Private Const CB_ERR = -1
'在ListBox或ComboBox中搜索指定字符串,并按照是否完全匹配,返回布尔值。
Public Function FindStringInListBoxOrComboBox(ByVal ctlControlSearch As Control,ByVal strSearchString As String,Optional ByVal blFindExactMatch As Boolean = True) As Boolean
On Error Resume Next
Dim lngRet As Long
If TypeOf ctlControlSearch Is ListBox Then
If blFindExactMatch = True Then
lngRet = SendMessage(ctlControlSearch.hWnd,LB_FINDSTRINGEXACT,-1,ByVal strSearchString)
Else
lngRet = SendMessage(ctlControlSearch.hWnd,LB_FINDSTRING,ByVal strSearchString)
End If
If lngRet = LB_ERR Then
FindStringInListBoxOrComboBox = False
Else
FindStringInListBoxOrComboBox = True
End If
ElseIf TypeOf ctlControlSearch Is ComboBox Then
If blFindExactMatch = True Then
lngRet = SendMessage(ctlControlSearch.hWnd,CB_FINDSTRINGEXACT,CB_FINDSTRING,ByVal strSearchString)
End If
If lngRet = CB_ERR Then
FindStringInListBoxOrComboBox = False
Else
FindStringInListBoxOrComboBox = True
End If
End If
End Function
'在ListBox或ComboBox中搜索指定字符串,并按照是否完全匹配,返回找到字符串所在的索引值。未找到返回 -1
Public Function GetStringIndexInListBoxOrComboBox(ByVal ctlControlSearch As Control,Optional ByVal blFindExactMatch As Boolean = True) As Long
On Error Resume Next
Dim lngRet As Long
GetStringIndexInListBoxOrComboBox = -1 '默认为 -1
If TypeOf ctlControlSearch Is ListBox Then
If blFindExactMatch = True Then
lngRet = SendMessage(ctlControlSearch.hWnd,ByVal strSearchString)
End If
GetStringIndexInListBoxOrComboBox = lngRet
ElseIf TypeOf ctlControlSearch Is ComboBox Then
If blFindExactMatch = True Then
lngRet = SendMessage(ctlControlSearch.hWnd,ByVal strSearchString)
End If
GetStringIndexInListBoxOrComboBox = lngRet
End If
End Function
用法示例:在窗体上加入:Command1,Command2,List1,List2
Private Sub Command1_Click()
MsgBox FindStringInListBoxOrComboBox(List1,"唐细",False) '模糊查找
MsgBox GetStringIndexInListBoxOrComboBox(List1,"唐细刚",True) '精确查找
End Sub
Private Sub Command2_Click()
MsgBox FindStringInListBoxOrComboBox(Combo1,False) '模糊查找
MsgBox GetStringIndexInListBoxOrComboBox(Combo1,True) '精确查找
End Sub
Private Sub Form_Load() List1.AddItem "aaa" List1.AddItem "bbbbb" List1.AddItem "唐细刚" List1.AddItem "ccccccccc" List1.AddItem "ddddddddddd" Combo1.AddItem "aaa" Combo1.AddItem "bbbbb" Combo1.AddItem "唐细刚" Combo1.AddItem "ccccccccc" Combo1.AddItem "ddddddddddd"End Sub
原文链接:https://www.f2er.com/vb/263245.html