我写了下面的代码,它从列表框中删除选定的值。我也想从字典列表中删除它,然后将其更新/写入文本文件,所以当我再次运行该程序并加载文本文件时,它将被更新,如果没有,它会继续显示删除的项目,每次我运行我的再次申请。为什么我得到一个异常InvalidOperationException?
private void listBox1_KeyDown(object sender, KeyEventArgs e)
{
string sb;
if (e.KeyCode == Keys.Delete)
{
if (this.listBox1.SelectedIndex >= 0)
{
string obj = this.listBox1.SelectedValue.ToString();
data.Remove(obj);
listBox1.DataSource = null;
listBox1.DataSource = data;
foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
{
for (int i = 0; i < kvp.Value.Count(); i++)
{
sb = "Url: " + kvp.Key + " --- " + "Local KeyWord: " + kvp.Value[i] + Environment.NewLine;
LocalyKeyWords.Remove(kvp.Key);
}
}
}
}
}
LocalyKeyWords是一个字典>
在这种情况下,它含有两个项目/键我删除一个和我具有断点,这一个已被删除看到。
的问题是,如果我需要删除kvp.Key或以某种方式删除我从列表框中删除至极的项目是一个我想从LocalyKeywords删除,它的obj变量,因为即时通讯做:
data.Remove(obj);
所以,也许我需要从localyKeyWords中删除obj?
错误即时得到的错误时抛出是它的去除的LocalyKeyWords的点击继续在这条线的项目后:
foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
即时得到错误:
集合被修改;枚举操作可能不会执行
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=Collection was modified; enumeration operation may not execute.
Source=mscorlib
StackTrace:
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
at GatherLinks.Form1.listBox1_KeyDown(Object sender, KeyEventArgs e) in d:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Form1.cs:line 959
at System.Windows.Forms.Control.OnKeyDown(KeyEventArgs e)
at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at GatherLinks.Program.Main() in d:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
你的两个for循环没有意义。您循环访问字典的键,然后遍历列表中的项并尝试为列表中的每个元素删除字典的键。这里有点不对劲。 – Steve 2013-02-24 23:11:48