Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching a listBox for a specified string VB6

I have a listbox called lstSerial and a textbox called txtSerials. What I want to do is search lstSerial for the string that's entered in txtSerials. I'm using VB6 in Microsoft Visual Basic 6.0, and I'm having a terrible time finding documentation.

Thanks.

like image 284
CurtisHx Avatar asked Feb 13 '12 18:02

CurtisHx


2 Answers

@AlexK's answer is technically correct - yes - it will work, but it's not the preferred way to go. There is an API call for this very purpose:

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

So you want to do this:

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

Source

like image 96
AngryHacker Avatar answered Sep 19 '22 21:09

AngryHacker


Docs; 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 ..."
like image 23
Alex K. Avatar answered Sep 16 '22 21:09

Alex K.