• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • 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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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