私は間違って何をしているのかわかりませんが、このエラーが発生し続けます。誰もがこれを引き起こす可能性があることを知っていますか?C#CheckedListBox foreachループのInvalidOperationException
InvalidOperationException この列挙子がバインドされているリストが変更されています。 列挙子は、リストが変更されない場合にのみ使用できます。リストが変更されない場合
public static string[] WRD = new string[] {"One","Two","Three","Four"}
public static string[] SYM = new string[] {"1","2","3","4"}
//this is how I'm populating the CheckedListBox
private void TMSelectionCombo_SelectedIndexChanged(object sender, EventArgs e)
{
TMSelection.Items.Clear();
switch (TMSelectionCombo.SelectedItem.ToString())
{
case "Words":
foreach (string s in WRD)
{
TMSelection.Items.Add(s);
}
break;
case "Symbols":
foreach (string s in SYM)
{
TMSelection.Items.Add(s);
}
break;
}
}
//this is where the problem is
private void AddTMs_Click(object sender, EventArgs e)
{
//on this first foreach the error is poping up
foreach (Object Obj in TMSelection.CheckedItems)
{
bool add = true;
foreach (Object Obj2 in SelectedTMs.Items)
{
if (Obj == Obj2)
add = false;
}
if (add == true)
TMSelection.Items.Add(Obj);
}
}
TMSelectionとは何ですか? foreachループ内にアイテムを追加している可能性があります。 –