2012-02-13 14 views
5

Tengo un cuadro de lista llamado lstSerial y un cuadro de texto llamado txtSerials. Lo que quiero hacer es buscar lstSerial para la cadena que se ingresa en txtSerials. Estoy usando VB6 en Microsoft Visual Basic 6.0, y me está costando encontrar documentación.Búsqueda en listBox para una cadena especificada VB6

Gracias.

Respuesta

7

@ respuesta de AlexK es técnicamente correcto - sí - que funcionará, pero no es la mejor forma de ir. Hay una llamada a la API para este mismo propósito:

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 

Así que quieres hacer esto:

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

Source

+0

+1 Esto se ejecuta mucho más rápido – MarkJ

6

Documentos; 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 utilizo este método también, nunca he encontrado su rendimiento ser malo así que lo prefiero a través de las llamadas API –

Cuestiones relacionadas