ListBox; get text from the list

Posted on 2009-02-12
Last Modified: 2012-05-06

I am wondering how I can get the text from a listbox. Lets say my list looks like this:
1       1       OK
1       2       OK
1       3       NOK

I want to go throw the list reading the third value in the list if it is an OK or an NOK. When I find NOK I will be happy and do something.

What should I use to get this to work?
Question by:Mickeys
    LVL 2

    Expert Comment

    You would use javascript.
    you have to get the index's text It's zero based, so 3 should be 2:
    to alert the text:

    <script type='text/javascript'>
    if(document.getElementById('THELISTID').options[2].text == 'NOK')
       //do something

    Author Comment

    Well maybe but I need to use C#
    LVL 2

    Expert Comment

    I was thinking

    From C#, here's index2:


    gives the text. It's basically like the javascript example I gave.
    LVL 29

    Accepted Solution

    Try this approach - see the snippet. You may add other separators like tab etc.
            private void button2_Click(object sender, EventArgs e)
                        foreach(string itemText  in listBox1.Items){
                            string[] separators = {" "};
                            string[] textArray = itemText.Split(separators, StringSplitOptions.RemoveEmptyEntries);

    Open in new window

    LVL 29

    Expert Comment

    Of course, if textArray[2] == "NOK" - you can do anything you want instead of displaying stupid MessageBox as I do :)
    LVL 13

    Expert Comment

    are you using listbox or listview?

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    733 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

    17 Experts available now in Live!

    Get 1:1 Help Now