简体   繁体   English

如何从与列表框中的项目相关的普通列表中删除项目?

[英]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 ? 如何删除与列表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. 我目前有一个public static List<Employees>和一个名为lstRecordslistbox ,我可以删除listbox的项目,但是可以从列表中删除所有内容,也可以完全删除任何内容。

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. 这是我正在使用的第二组代码,我的List属于部分类,但这全部包含在方法中。

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. 您要做的是将ListBox绑定到“员工列表”。 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> 将列表框绑定到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. DeleteRecord()方法的问题在于,您刚创建的lstRecords对象不是窗体上的ListBox。

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. 第一个代码块确定,但是我不知道您的lstRecords在哪里。 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. 它在代码中创建一个列表框,然后将其添加到窗体(Winforms)中并具有lstRecords全局属性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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