7

我编写了以下代码,它从列表框中删除了选定的值。我还想将它从字典列表中删除,然后将其更新/写入文本文件,这样当我再次运行程序并加载文本文件时,它将被更新,如果不是,它将在每次运行时继续显示已删除的项目再次申请。

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 或以某种方式删除我从 listBox 中删除的项目,这是我想从 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: 
4

2 回答 2

11

您正在从LocalyKeyWords迭代中删除项目;正如异常消息所说,这是不允许的。

我不确定这里的大局是什么,但一个本地化的解决方案是制作一个临时副本LocalyKeyWords并对其进行迭代。然后,您可以毫无困难地修改“源”集合。

例子:

foreach (var kvp in LocalyKeyWords.ToList())  // .ToList() makes a temp copy
于 2013-02-24T23:02:08.410 回答
1

您不能在foreach.

您可以创建字典的副本并循环修改原始字典。

于 2013-02-24T23:04:03.040 回答