VB6在ListBox或Combox中搜索字符串项的模块(支持模糊与精确查找)


'****************************************************************************
'模块名称: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

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...