gpt4 book ai didi

c# - 设置 DataSource 时从 ListBox 中删除项目

转载 作者:行者123 更新时间:2023-12-04 02:58:10 28 4
gpt4 key购买 nike

请参阅我有一个具有多个值的 HashSet,该值可以包含例如 4141234567 4241234567 _0x10456790719 4141234567 _0x104567907907 和 _0x10456790770700x45670 等数字。我在我的 UserControl 中放置了一个 radioButton1,当我点击它时,我希望只有 414 和 424 的数字保留在我的 ListBox 中,为此我编写了以下代码:

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
var bdHashSet = new HashSet<string>(bd);

if (openFileDialog1.FileName.ToString() != "")
{
foreach (var item in bdHashSet)
{
if (item.Substring(1, 3) != "414" || item.Substring(1, 3) != "424")
{
listBox1.Items.Remove(item);
}
}
}
}

但是当我运行代码时,我收到此错误:

Items collection cannot be modified when the DataSource property is set.



从列表中删除不需要的项目而不从 HashSet 中删除它们的正确方法是什么?我稍后会为以 0416 和 0426 开头的数字添加一个 optionButton 以及一个 optionButton 以使用原始值填充列表框,有什么建议吗?

最佳答案

尝试

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
var bdHashSet = new HashSet<string>(bd);

listBox1.Datasource = null;
listBox1.Datasource = bdHashSet.Where(s => (s.StartsWith("414") || s.StartsWith("424"))).ToList();
}

关于c# - 设置 DataSource 时从 ListBox 中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16028133/

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