我有一个使用绑定列表作为数据源的 C# Winform,其中包含一个列表框。该列表由计算机上的文本文件创建。我正在尝试为此列表框创建一个“全部删除”按钮,但遇到了一些问题。
首先,这是相关代码:
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);
}
}
我尝试使用foreach绑定源,但它会给出枚举错误并且无法工作。据我所知,没有代码可以清除整个源,因此我尝试通过遍历列表本身并通过项名称删除它们,但是这也不起作用,因为foreach实际上返回一个对象或其他东西而不是字符串。
您有如何解决这个问题的建议吗?
Clear()
方法吗? - LightStriker