【问题标题】:remove delete item from listbox list of items从项目列表框列表中删除删除项目
【发布时间】:2013-07-05 11:36:18
【问题描述】:

我有一个包含项目集合的列表框。从列表框中删除项目后,我想从列表框中删除该项目而不重新加载整个集合,这在 winforms 中是否可行?

 private void btnDelete_Click(object sender, EventArgs e)
        {
            MyData sel = (MyData)listBox1.SelectedItem;
            if (...delete data)
            {
                listBox1.Items.Remove(listBox1.SelectedItem);
                MessageBox.Show("succ. deleted!");
            }
            else
            {
                MessageBox.Show("error!");
            }           
        }

我遇到了错误

当数据源属性为 设置

【问题讨论】:

    标签: .net winforms


    【解决方案1】:

    嘿,尝试从您的集合中获取选定项目索引,然后按索引删除项目表单集合,然后再次将您的列表框绑定到集合..

    我已经做了示例代码,请参考。

    public partial class Form1 : Form
    {
        List<String> lstProduct = new List<String>();
        public Form1()
        {
    
            InitializeComponent();
        }
    
        public List<String> BindList()
        {
    
            lstProduct.Add("Name");
            lstProduct.Add("Name1");
            lstProduct.Add("Name2");
            lstProduct.Add("Nam3");
            lstProduct.Add("Name4");
    
            return lstProduct;
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.DataSource = BindList();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            // The Remove button was clicked.
            int selectedIndex = listBox1.SelectedIndex;
    
            try
            {
                // Remove the item in the List.
                lstProduct.RemoveAt(selectedIndex);
            }
            catch
            {
            }
    
            listBox1.DataSource = null;
            listBox1.DataSource = lstProduct;
        }
    }
    

    希望对你有所帮助....

    【讨论】:

    • 需要使用 ObservableCollection 通知 UI 删除
    • @Blam 据我所知 ObservableCollection 用于 WPF 应用程序而不是传统的 Win 形式。
    • 感谢这篇文章,尝试过,但再次加载整个列表时遇到性能问题。
    【解决方案2】:

    您应该使用可观察的集合作为DataSource。 您可以使用内置的,例如BindingList&lt;T&gt;ObservableCollection&lt;T&gt;

    但您也可以考虑创建自己的集合并实现IBindingListINotifyCollectionChanged 接口。

    更新

    public partial class YourForm : Form
    {
        private BindingList<string> m_bindingList = new BindingList<string>();
    
        private YourForm()
        {
            InitializeComponent();
            yourListBox.DataSource = m_bindingList;
    
            // Now you can add/remove items to/from m_bindingList
            // and these changes will be reflected in yourListBox.
    
            // But you shouldn't (and can't) modify yourListBox.Items
            // as long as DataSource is set.
        }
    
        private void btnDelete_Click(object sender, EventArgs e)
        {
            // Removing items by indices is preferable because this avoids
            // having to lookup the item by its value.
            m_bindingList.RemoveAt(yourListBox.SelectedIndex);
        }
    }
    

    【讨论】:

    • 您能否详细说明此示例的具体提示。我尝试使用@Nerraj Dubey 答案示例,但性能真的很差。
    猜你喜欢
    • 2020-12-07
    • 2010-11-29
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多