This is my first time using this site so hopefully I ask my question properly.
I am trying to make a program that has a BindingList of RentalCar objects. Now I am trying to allow myself to remove multiple cars at once.
This is the code i have currently for the remove button.
private void buttonRemoveRental_Click(object sender, EventArgs e)
{
try
{
//List<RentalCar> tempList = new List<RentalCar>(); (This was here for another solution i am trying)
int index = listBoxRental.SelectedIndex;
for (int i = rentalList_.Count - 1; i >= 0; i--)
{
if (listBoxRental.SelectedIndices.Contains(i))
{
rentalList_.RemoveAt(i);
}
}
}
catch(Exception)
{
MessageBox.Show("Please select a vehicle to remove from the list");
}
But sometimes one item will be left in the listbox which i can't delete. And every single time if i try deleting the last item, it deletes every item from the list.
Another solution i am trying was to create another list, which will store the selected vehicles from my rentalList_ and then loop through and delete the items in the tempList from the rentalList_ But i don;t know how to go about that because i am storing objects.