2012-02-13 16 views
5

Mam listbox o nazwie lstSerial i pole tekstowe o nazwie txtSerials. Co chcę zrobić, to przeszukaj lstSerial dla ciągu znaków, który jest wprowadzony w txtSerials. Używam VB6 w Microsoft Visual Basic 6.0 i mam strasznie dużo czasu na znajdowanie dokumentacji.Wyszukiwanie listbox dla określonego ciągu VB6

Dzięki.

Odpowiedz

7

@ odpowiedź AlexK jest technicznie poprawne - tak - to będzie działać, ale nie jest najlepszym sposobem, aby przejść. Jest wywołanie API dla tego samego celu:

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 

Więc chcesz to zrobić:

lstSerial.ListIndex = GetListBoxIndex(lstSerial.hWnd, txtSerials.Text) 

Source

+0

+1 To działa znacznie szybciej – MarkJ

6

Dokumenty; http://msdn.microsoft.com/en-us/library/aa267225(v=VS.60).aspx

dim find as string,i as long,found as boolean 
find=txtSerials.text 

for i=0 to lstserial.listcount - 1 
    if strcomp(find, lstSerial.list(i), vbTextcompare)=0 then 
     found = true 
     lstSerial.setfocus 
     lstSerial.listindex= i 
     exit for 
    end if 
next 

if not found then msgbox "not found ..." 
+1

+1 Używam tej metody zbyt, nigdy nie okazało się, że to wykonanie być kiepskim, więc preferuj go przez wywołania API –