How to delete a selected listbox item from both the listbox and the original list in c#

2.3k Views Asked by At

I'm having problems removing the data in a list which was entered by a user in my textboxes and then stored in a listbox. I know how to remove the selected item in the listbox but when I click my button to show everything in the list, the selected item I just deleted is still in the list.

Here is my code for removing the selected item in the listbox:

for (int i = 0; i < VehicleListBox.SelectedItems.Count; i++)
    VehicleListBox.Items.Remove(VehicleListBox.SelectedItems[i]);

My list is in a class called company and the list is named vehicles. I have looked everywhere for the most part for my answer and cannot seem to find it. I should also mention it's a generic list.

4

There are 4 best solutions below

3
Rowbear On BEST ANSWER

With your class design as I understand it, I wrote the following code in a WPF project. I added a listbox called "listbox1" and a button to the form. I believe the following code does what you want, or at least would guide you to the answer.

    public class Company
    {
        public List<Vehicle> Vehicles;
        public Company()
        {
            Vehicles = new List<Vehicle>() { new Vehicle(1), new Vehicle(2), new Vehicle(3) };
        }
    }
    public class Vehicle
    {
        private string _vehicleNum;
        public Vehicle(int num)
        {
            _vehicleNum = "Vehicle" + num.ToString();
        }
        public string getDetails()
        {
            return _vehicleNum;
        }
    }
    Company ACompany = new Company();
    public MainWindow()
    {
        InitializeComponent();

        foreach(Vehicle v in ACompany.Vehicles)
            listbox1.Items.Add(v.getDetails());

    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < listbox1.SelectedItems.Count; i++)
        {
            foreach(Vehicle v in ACompany.Vehicles)
            {
                if (String.Equals(v.getDetails(), listbox1.SelectedItems[i].ToString()))
                {
                    ACompany.Vehicles.Remove(v);
                    break;
                }
            }
            listbox1.Items.Remove(listbox1.SelectedItems[i]);
        }
    }
2
SJP On

Ok this works I tested it.

foreach (string thing in listBox1.SelectedItems){
                myList.remove(thing);
            }
4
Algamest On

So as I understand you're using getDetails to get the list, then one-by-one adding them to the VehicleListBox.

An option would be to remove the selected items from the List and then update the ListBox accordingly.

You add a simple method to do this:

private void UpdateListBox(List<string> vehicles);

Replacing `List' with the type you're using.

Alternatively have you tried binding the ItemsSource of the ListBox?

In WPF (for example):

<ListBox Name=ExampleListBox, ItemsSource={Binding} />

The in code:

ExampleListBox.DataContext = myList;

This best goes in the Window_Loaded method after you've populated the List. If necessary then update this when the list changes.

1
Grant Winney On

Given your current code, you could parse the string back out by reversing whatever getDetails() is doing, and then select the appropriate object from your list.

for (int i = 0; i < VehicleListBox.SelectedItems.Count; i++)
{
    var item = (string)VehicleListBox.SelectedItems[i];

    VehicleListBox.Items.Remove(item);

    // assuming your company class has only a Name and ID...
    string name = ...  // parse name from item
    int id = ...       // parse id from item

    vehicles.Remove(vehicles.Single(x => x.Name == name && x.ID == id));
}