Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Selected Items From WinForm ListBox?

I have a ListBox in a WinForm with multiselect enabled.

The selected items appear to be stored in an object, how to I get their values?

like image 245
sooprise Avatar asked Jun 02 '10 18:06

sooprise


2 Answers

Easy, depending on what type you stored:

foreach (MyItemType item in listBox1.SelectedItems)
{
   ...
}

Because this is an older, non-generic collection it is better not to use var to declare the item variable. That would only get you a reference of type object.

You can also use other properties like:

if (listBox1.SelectedItems.Count > 0)
   ...
like image 117
Henk Holterman Avatar answered Oct 25 '22 02:10

Henk Holterman


Just use the following code to display the selected item from the ListBox - for WinForm app...

string s = listbox1.Text; //replace listbox1 with your listbox control
like image 24
Ephrem Avatar answered Oct 25 '22 02:10

Ephrem