• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

ListBox; get text from the list

Hi,

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?
0
Mickeys
Asked:
Mickeys
1 Solution
 
pandalandalandCommented:
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'>
alert(document.getElementById('THELISTID').options[2].text);
</script>
So:
if(document.getElementById('THELISTID').options[2].text == 'NOK')
   //do something
0
 
MickeysAuthor Commented:
Well maybe but I need to use C#
0
 
pandalandalandCommented:
I was thinking asp.net.

From C#, here's index2:

listName.Items[2].Text;

gives the text. It's basically like the javascript example I gave.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
anarki_jimbelCommented:
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);
                        MessageBox.Show(textArray[2]);
                    }
        }

Open in new window

0
 
anarki_jimbelCommented:
Of course, if textArray[2] == "NOK" - you can do anything you want instead of displaying stupid MessageBox as I do :)
0
 
drypzCommented:
are you using listbox or listview?
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now