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?
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)
...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With