简体   繁体   中英

How to remove item from generic list that relates to item in listbox?

I have searched around but could not find any references.

How do I delete an item in a generic list that relates to items in a listbox ?

I currently have a public static List<Employees> and a listbox named lstRecords , I can remove the item in the listbox just fine, but either everything is removed from the list or nothing at all.

This was my first set of code I was working with:

private void DeleteRecord()
{
        if (lstRecords.Items.Count > 0)
        {
            for (int i = 0; i < lstRecords.Items.Count; i++)
            {
                if (lstRecords.GetSelected(i) == true)
                {
                    Employees employeeRecord = lstRecords.SelectedItem as Employees;
                    employee.Remove(employeeRecord);
                }
            }



            lstRecords.Items.Remove(lstRecords.SelectedItem);
        }
    }  
}

This is my 2nd set of code I was working with, I have my List right under partial class, but this is all contained in a method.

private void DeleteRecord()
{
        ListBox lstRecords = new ListBox();
        List<object> employee = new List<object>();
        employee.RemoveAt(lstRecords.SelectedIndex);
        lstRecords.Items.RemoveAt(lstRecords.SelectedIndex);
}  

So far I haven't gotten either set of code to work the way I would like it to, I'm obviously doing something wrong.

I have a few other blocks of code I played around with but these seemed to be headed in the right direction.

Eventually I'll need to be able to double click an item in the list to pull up the properties menu.

What you want to do is bind your ListBox to you List of employees. This post shows the binding and the comments shows the removing code as well. The idea is that when you remove an item from the DataSource, then you won't see it in the ListBox.

Binding Listbox to List<object>

The problem with the DeleteRecord() method is that the lstRecords object you just created isn't the ListBox that is on the form.

Your code runs fine you just have to make some small changes. The first code block is Ok however I dont know where your lstRecords are. But have a look at this just copy the code and run it after you have some records in your employee object.

It's createing a listbox in code then adds it to the form(Winforms) and having the lstRecords globaly.

ListBox lstRecords;
    private void IntializeDemoListbox()
    {
        lstRecords = new ListBox();
        this.Controls.Add(lstRecords);

        foreach (var item in employee)
        {
            lstRecords.Items.Add(item);
        }
    }

And then you will be able to use your first set of code the other set will be like this.

private void DeleteRecord()
{
        employee.RemoveAt(lstRecords.SelectedIndex);
        lstRecords.Items.RemoveAt(lstRecords.SelectedIndex);
}  

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