C# CheckedListBox移除打钩项的问题
我使用OldCheckListBox.Items.Remove(OldCheckListBox.CheckedItems[i]);函数只能把值去掉,然后又出现异常了,说索引超出了数组界限,怎么解决这个问题,
public static void GetListFromCheckListBoxToAnother(CheckedListBox OldCheckListBox, CheckedListBox TargetCheckListBox)c#
{
for (int i = 0; i < OldCheckListBox.CheckedItems.Count; i++)
{
bool CheckItem = false;
for (int j = 0; j < TargetCheckListBox.Items.Count; j++)
{
if (OldCheckListBox.CheckedItems[i].ToString()==TargetCheckListBox.Items[j].ToString())
{
CheckItem = true;
break;
}
}
if (CheckItem == true)
{
}
else
{
OldCheckListBox.Items.Remove(OldCheckListBox.CheckedItems[i]);
TargetCheckListBox.Items.Add(OldCheckListBox.CheckedItems[i].ToString());
}
}
}