Getting Selected Items From WinForm ListBox?
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)
...