简体   繁体   中英

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

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.

Ok this works I tested it.

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

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.

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]);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM