我已经通过在这里阅读您的优秀答案了解到,从foreach循环中删除项目并不是一个好习惯,因为它是(我引用)“关闭分支你坐在“。将foreach循环转换为while循环的帮助#
我的代码当前从下拉列表中删除文本,但实际项目仍然存在(仅显示文本)。
换句话说,它不是删除,而且可能不能,因为你不能从foreach循环中删除。
经过几个小时的尝试,我无法让我的脑子里想到这样做。
//For each checked box, run the delete code
for (int i = 0; i < this.organizeFav.CheckedItems.Count; i++)
{
//this is the foreach loop
foreach (ToolStripItem mItem in favoritesToolStripMenuItem.DropDownItems)
{
//This rules out seperators
if (mItem is ToolStripMenuItem)
{
ToolStripMenuItem menuItem = mItem as ToolStripMenuItem;
//This matches the dropdownitems text to the CheckedItems String
if (((ToolStripMenuItem)mItem).Text.ToString() == organizeFav.CheckedItems[i].ToString())
{
//And deletes the item
menuItem.DropDownItems.Remove(mItem);
}
}
}
}
但它不是删除,因为它在一个foreach循环内! 我将非常感谢您的帮助,并且是真正的惊讶,如果任何人都可以解决此代码:)
亲切的问候
是的,Linq也是我的首选,但我不想吓倒OP,它的超棒力量:) – slugster 2010-03-24 08:35:12