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

本文介绍了一个用于VB6的模块,该模块能够在ListBox或ComboBox控件中进行字符串搜索,支持精确和模糊查找。提供了两个示例函数,分别用于检查字符串是否存在以及返回字符串的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


'****************************************************************************
'模块名称:mListBoxComboBoxSearch.bas
'发布日期:2009/03/06
'描    述:VB6在ListBox或Combox中搜索字符串项的模块(支持模糊与精确查找)
'博    客:http://blog.youkuaiyun.com/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 wMsg 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, -1, 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, -1, ByVal strSearchString)
       Else
          lngRet = SendMessage(ctlControlSearch.hWnd, CB_FINDSTRING, -1, 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, ByVal strSearchString As String, 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, LB_FINDSTRINGEXACT, -1, ByVal strSearchString)
       Else
          lngRet = SendMessage(ctlControlSearch.hWnd, LB_FINDSTRING, -1, ByVal strSearchString)
       End If
       GetStringIndexInListBoxOrComboBox = lngRet
    ElseIf TypeOf ctlControlSearch Is ComboBox Then
       If blFindExactMatch = True Then
          lngRet = SendMessage(ctlControlSearch.hWnd, CB_FINDSTRINGEXACT, -1, ByVal strSearchString)
       Else
          lngRet = SendMessage(ctlControlSearch.hWnd, CB_FINDSTRING, -1, 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.cnblogs.com/it201108/archive/2009/03/06/2148111.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值