If no item is selected in the ListBox, then the code works fine.
If at least one item is selected in the ListBox, the foreach iteration breaks after the first item is evaluated. The exception is an InvalidOperationException and the detail shows Items collection has been modified.
foreach (object item in listBoxFiles.Items) //InvalidOperationException occurs
{
if (listBoxFiles.SelectedItems.Contains(item))
{
//do nothing
}
}
Edit: I was looking for something like ListBoxItem.IsSelected but it does not exist.
//do nothingdoes nothing to the item collection?