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 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

 

 

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值