gpt4 book ai didi

C# - 从列表框中删除所有项目

转载 作者:行者123 更新时间:2023-11-30 19:10:42 25 4
gpt4 key购买 nike

我有一个 C# winform,它使用一个带有数据源绑定(bind)列表的列表框。该列表是从计算机上的文本文件创建的。我正在尝试为此列表框创建一个“全部删除”按钮,但遇到了一些麻烦。

首先是相关代码:

private void btnRemoveAll_Click(object sender, EventArgs e)
{
// Use a binding source to keep the listbox updated with all items
// that we add
BindingSource bindingSource = (BindingSource)listBox1.DataSource;

// There doesn't seem to be a method for purging the entire source,
// so going to try a workaround using the main list.
List<string> copy_items = items;
foreach (String item in copy_items)
{
bindingSource.Remove(item);
}
}

我试过 foreaching bindingSource,但它给出了一个枚举错误并且无法正常工作。据我所知,没有代码可以清除整个源,所以我尝试遍历 List 本身并通过项目名称删除它们,但这也不起作用,因为 foreach 实际上返回了一个对象或其他东西,并且不是字符串。

关于如何做到这一点有什么建议吗?

最佳答案

直接输入即可

listBox1.Items.Clear();

关于C# - 从列表框中删除所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16240003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com