Finding a string in a listbox

Posted on 2006-05-29
Last Modified: 2010-04-23
Hi Experts

In VB.NET i have a listbox (lstData) which has the name and Email address of customers on
each row,using a textbox (txtSearch) i can search the listbox for a customer name and have
it selected but i am unable to figure out the code to search for the customers Email address
(because i think it is not at the begining of each row or index).

Please can you help
Question by:dizzycat
    LVL 17

    Accepted Solution

    What do you have separating the customer name and email address?  Are there a couple spaces, a character (like "/"), or a tab?  If so, you can do a string comparision for the Email address using the .EndsWith method.

    'Example, if you have items in your listbox like:  "John Doe /"

            Dim strSeparator As String = "/ "
            For i As Integer = 0 To lstData.Items.Count - 1
                If CStr(lstData.Items.Item(i)).EndsWith(strSeparator & txtSearch.Text) Then
                    MsgBox("Customer found at index: " & i)
                    Exit Sub
                End If

            MsgBox("Customer not found")

    You can use .StartsWith to search only the customer's Names.

    Author Comment

    Thanks ZeonFlash

    I have used lstData.SelectedIndex = (i) together with your code and it works perfectly.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now