i have a listbox and i want to delete an object in collection with this listbox . but i can only just delete the first item (select index 0 ) why ?? i can't solve this problem
private void removeButton_Click(object sender, EventArgs e)
{
foreach (Student element in studentCollection) {
if (studentListbox.SelectedIndex != -1 && element.Name == studentListbox.SelectedItem.ToString())
{
studentCollection.Remove(element);
studentListbox.Items.RemoveAt(studentListbox.SelectedIndex);
}
break;
}
}