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?
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:
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 typeobject
.You can also use other properties like:
The selected items are found in the
SelectedItems
property. These are the objects that you added to the list box, so you can cast the objects to their respective type and access any members that way:Try the SelectedItems property.
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