2013-05-01 28 views
0

我有一個包含幾千項的列表框。如果我想獲得第一場比賽,以下由@AngryHacker in this threat給出的代碼很適合。但有時我有多個項目具有相同的數據。所以,我想要得到所有的比賽,怎麼做?VB6:如何使用API​​搜索列表框並獲取所有匹配?

呵呵,其實,它是這樣的: 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 
+0

只是出於好奇,爲什麼你會在列表框中有很多項目?該列表框用於向用戶提供可供選擇的選項,並且通過數千個滾動列表框似乎是一個非常漫長的過程。 (首先允許重複也不是一個明智的用戶界面決定,因爲如果你有兩個選擇是相同的,你不能確定他們選擇了哪一個,如果你不需要告訴他們,那麼你不需要在第一位顯示它們。) – 2013-05-01 21:00:54

+0

好的,列表框對於太多的項目來說太糟糕了,但是我還沒有找到任何其他解決方案,因爲我使用的是vb6 。我想知道是否有替代列表框 – 2013-05-01 21:06:59

+0

您仍然沒有回答他的問題。列表框是用於用戶界面的。爲什麼你需要在你的列表框中有成千上萬的項目? – 2013-05-01 21:10:17

回答

3

您可以利用wParam代表LB_FINDSTRINGLB_FINDSTRINGEXACT消息讓主叫方指定要搜索的第一個項目:

的wParam

要搜索的第一個項目之前的項目的索引(從零開始)。當搜索到達列表框的底部時,它將繼續從列表框的頂部返回到由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 
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 

我必須尋找debtor_code每個債務人有多個地址,我需要在組合框中得到多個答案

相關問題