Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search listbox 3

Status
Not open for further replies.

jono261970

Programmer
Jun 28, 2002
182
GB
I have a list box populated with over a 1000 names. Is there a way to search this list for a particular name. I thought about using a for next loop which would compare the contents of a text box against the list box. Would this be slow and is there a better way.

Thanks in advance.

Jono


A good fortune may forbode a bad luck, which may in turn disguise a good fortune.
 
I would say a For .. Next loop is the way to go.

I'd make sure that the TextBox and List item are the same case too. (I once spent several hours debugging a listbox search routine until I realised I was comparing "smith" with "Smith" with the usual predictable and hilarious result.)[smile]

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 

Try this:
[blue]
Code:
Option Explicit
Private Const LB_FINDSTRING = &H18F
Private Const LB_FINDSTRINGEXACT = &H1A2
Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

Public Sub FindListItem(ByRef lst As VB.ListBox, ByVal sSearchValue As String, Optional ExactMatch As Boolean = True)
    Dim iIndex As Integer
    Dim MatchFlag As Long
    
    If ExactMatch Then
        MatchFlag = LB_FINDSTRINGEXACT
    Else
        MatchFlag = LB_FINDSTRING
    End If
    
    iIndex = SendMessageString(lst.hwnd, MatchFlag, -1&, sSearchValue)
    If iIndex > -1 Then lst.ListIndex = iIndex
End Sub
[black]

Call FindListItem (List1, "AbC")
 
wow! thanks for the example clint!

this is much faster than my for next loop!

jono


A good fortune may forbode a bad luck, which may in turn disguise a good fortune.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top