C# 已修改此枚举数绑定到的列表。仅当列表未更改时,才能使用枚举数

C# 已修改此枚举数绑定到的列表。仅当列表未更改时,才能使用枚举数,c#,winforms,checkedlistbox,C#,Winforms,Checkedlistbox,我创建了下面的代码,它从未给我任何错误: foreach (var item in clb_frente_campos.CheckedItems) { if (item == "numero_carteira") { clb_frente_impressao.Items.Add(item); break; } else { if (!clb_frente_impressao.Items

我创建了下面的代码,它从未给我任何错误:

foreach (var item in clb_frente_campos.CheckedItems)
{
    if (item == "numero_carteira")
    {            
        clb_frente_impressao.Items.Add(item);
        break;
    }
    else
    {
        if (!clb_frente_impressao.Items.Contains(item))
        {
            clb_frente_impressao.Items.Add(item);
            break;
        }
    }
}
此代码是一个按钮,用于将
选中的
值从一个
选中列表框
传递到另一个。
请注意,当项目
numero\u carteira
被选中时,它将被一次又一次地添加到我的其他
CheckedListBox
。因为项目
numero_carteira
是唯一可以多次添加的项目。但我只希望在选中
时添加,这就是为什么我制作了这个新代码:

foreach (var item in clb_frente_campos.CheckedItems)
{
    if (item == "numero_carteira")
    {
        if (clb_frente_campos.SelectedItem.ToString() == "numero_carteira")
        {
            clb_frente_impressao.Items.Add(item);
            break;
        }
    }
    else
    {
        if (!clb_frente_impressao.Items.Contains(item))
        {
            clb_frente_impressao.Items.Add(item);
            break;
        }
    }
}
而这个新代码正是标题中给我的
错误


也是我见过的最奇怪的事情。当我使用断点并一步一步地调试它时。。。它是如何工作的?为什么

如果它在使用调试器时起作用,那么几乎肯定会出现线程问题。在没有看到其余代码的情况下进行调试是很困难的,但我的直觉是寻找其他您可能循环使用的地方。
clb_frente_impressao.Items

这也是我的感觉,但这似乎是一个注释,而不是答案。但这就是我的全部代码。。。所有这些都在一个
ImageButton\u Click
Event.@Ghaleon您在任何地方都不使用
clb\u frente\u impressao
?那么,拥有它有什么意义呢?@MikeCaron它只是保存了我从
clb\u frente\u campos
中选择的物品。你知道的?最初,
clb_frente_campos
完全为空,然后我开始从“clb_frente_campos”中选择项目,并将这些项目添加到
clb_frente_campos
。错误发生在
ForEach
上。如果你要否决它,请至少有勇气在这里公布原因。谢谢