1

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.

3
  • what type of list is rentalList ? Commented Jan 16, 2013 at 3:15
  • That's because you are using a bad iteration. When you RemoveAt(i), the content of the list is being changed and the next RemoveAt(i) will be shifted. Commented Jan 16, 2013 at 3:17
  • rentalList is a binded list to the listBox. rentalList_ = new BindingList<RentalCar>(); listBoxRental.DataSource = rentalList_; Commented Jan 16, 2013 at 3:22

2 Answers 2

1

Try this solution. It's working fine on me.

 private void buttonRemoveRental_Click(object sender, EventArgs e)
    {
       var selectedItems= listBoxRental.SelectedItems.Cast<String>().ToList();
       foreach (var item in selectedItems)
            listBoxRental.Items.Remove(item);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I just had to change the .Cast<String> to .Cast<RentalCar> and i had to use rentalList_.Remove(item) because i got an error saying i couldn't change the data of the listbox or something. But it works now. Thanks. If i could i would vote up for this. But i don't have enough reputation yet.
You don't need to vote up mate. If you feel that my solution is suitable to your problem you can "Accept" it as answer. Cheers
1

When you loop and remove item from same List, you are removing wrong item at wrong index because index will be reset after you remove an item.

Try this

List<RentalCar> tempList = new List<RentalCar>();
for (int i = 0; i <=rentalList.Count - 1; i++)
{
    if (!listBoxRental.SelectedIndices.Contains(i))
    {
       tempList.Add(rentalList[i]);
    }
}

You can then bind tempList to ListBox

1 Comment

This works. Except since my program can also add vehicles to the list, that means i can't add any vehicles because the old list isn't binded to the listBox anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.