0

我有一个包含几千个项目的列表框。如果我想获得第一场比赛, @AngryHacker 在此威胁中给出的以下代码非常适合。但有时我有多个项目具有相同的数据。所以,我想得到所有的比赛,怎么做?

哦,实际上,它是这样的:aa4 sds aa5 aa6 fdf dsf

从列表中,我想获取以“aa”开头的项目的索引

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

'constants for searching the ListBox
Private Const LB_FINDSTRINGEXACT = &H1A2
Private Const LB_FINDSTRING = &H18F

'function to get find an item in the Listbox
Public Function GetListBoxIndex(hWnd As Long, SearchKey As String, Optional FindExactMatch As Boolean = True) As Long

    If FindExactMatch Then
        GetListBoxIndex = SendMessage(hWnd, LB_FINDSTRINGEXACT, -1, ByVal SearchKey)
    Else
        GetListBoxIndex = SendMessage(hWnd, LB_FINDSTRING, -1, ByVal SearchKey)
    End If

End Function
4

2 回答 2

4

您可以利用LB_FINDSTRINGLB_FINDSTRINGEXACTwParam消息让调用者指定要搜索的第一个项目的事实:

参数

要搜索的第一个项目之前的项目的从零开始的索引。当搜索到达列表框底部时,它会从列表框顶部继续搜索到 wParam 参数指定的项目。如果 wParam 为 – 1,则从头开始搜索整个列表框。

因此,您GetListBoxIndex采用以下形式(注意StartIndex参数而不是硬编码-1):

'LB_ constants
Private Const LB_ERR = -1
Private Const LB_FINDSTRINGEXACT = &H1A2
Private Const LB_FINDSTRING = &H18F

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

Public Function GetListBoxIndex(hWnd As Long _
    , SearchKey As String _
    , StartIndex As Long _
    , Optional FindExactMatch As Boolean = True) As Long
    If FindExactMatch Then
        GetListBoxIndex = SendMessage(hWnd, LB_FINDSTRINGEXACT, StartIndex, SearchKey)
    Else
        GetListBoxIndex = SendMessage(hWnd, LB_FINDSTRING, StartIndex, SearchKey)
    End If
End Function

其余的取决于您打算如何处理之后的结果。以下是仅将结果打印到即时窗口的简单测试:

Private Sub Command1_Click()
    PrintAllMatches List1.hWnd, Text1.Text
End Sub

Private Sub Form_Load()
    List1.AddItem "aa1"
    List1.AddItem "bbb"
    List1.AddItem "aa2"
End Sub

Private Sub PrintAllMatches(hWnd As Long, SearchKey As String)
    Dim firstMatch As Long, nextMatch As Long
    nextMatch = GetListBoxIndex(hWnd, SearchKey, -1, False)
    If nextMatch = LB_ERR Then
        Debug.Print "Not found"
        Exit Sub
    End If

    firstMatch = nextMatch
    Do
        Debug.Print "Match is at index " & nextMatch
        nextMatch = GetListBoxIndex(hWnd, SearchKey, nextMatch, False)
    Loop While nextMatch <> firstMatch
End Sub
于 2013-05-01T21:56:09.137 回答
0

我有类似的情况如何解决以下代码,

Adodc1.Recordset.MoveFirst Adodc1.Recordset.Find "DEBTOR_CODE = '" & Text11.Text & "'" If Adodc1.Recordset.EOF = True Or Adodc1.Recordset.BOF = True Then MsgBox "Record Not Found!", vbApplicationModal Adodc1.Recordset.MoveFirst Me.Combo1.SetFocus Me.Combo1.ListIndex = Me.Text11.Text End If

我必须搜索债务人代码,每个债务人都有多个地址,我需要在组合框中获得多个答案

于 2014-11-14T04:45:17.117 回答