This code isn’t working. It doesn’t raise an exception or even do anything visible.
private void RemoveSelectedFiles()
{
lstPhotos.Items.Remove(lstPhotos.SelectedItems);
}
How can I remove the selected items from a ListBox?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You have to remove one item at a time.
EDIT – as @Smith pointed, the code would raise an exception because
ListBox.SelectedItemsis bound to theItemscollection. Removing a selected item fromItemswill effectively remove it fromSelectedItemstoo, thus breaking the enumeration. Now we enumerate an independent list containing the selected items: