Why I'm getting an exception InvalidOperationException?
<p>I have this code i'm removing the selected value from the listBox but I also want to remove it from the Dictionary List and then update/rewrite it to the text file so when I run the program again and load the text file it will be updated if not it will keep showing the removed item each time I run my application again.</p>

<pre><code>private void listBox1_KeyDown(object sender, KeyEventArgs e)
        {
            string sb;
            if (e.KeyCode == Keys.Delete)
            {
                if (this.listBox1.SelectedIndex &gt;= 0)
                {
                    string obj = this.listBox1.SelectedValue.ToString();
                    data.Remove(obj);
                    listBox1.DataSource = null;
                    listBox1.DataSource = data;
                    foreach (KeyValuePair&lt;string, List&lt;string&gt;&gt; kvp in LocalyKeyWords)
                    {
                        for (int i = 0; i &lt; kvp.Value.Count(); i++)
                        {
                            sb = "Url: " + kvp.Key + " --- " + "Local KeyWord: " + kvp.Value[i] + Environment.NewLine;
                            LocalyKeyWords.Remove(kvp.Key);
                        }
                    }

                }
            }

        }
</code></pre>

<p>LocalyKeyWords is a Dictionary></p>

<p>In this case it contain two items/keys I remove one and I see with a breakpoint that this one have been removed. </p>

<p>The question is if I need to remove the kvp.Key or to remove somehow the item I removed from the listBox wich is the one I want to remove from the LocalyKeywords and it's the obj variable since im doing:</p>

<pre><code>data.Remove(obj);
</code></pre>

<p>So maybe I need to remove obj also from the localyKeyWords ?</p>

<p>The excpetion the error im getting is after it's removing the item from the LocalyKeyWords an click continue on this line:</p>

<pre><code>foreach (KeyValuePair&lt;string, List&lt;string&gt;&gt; kvp in LocalyKeyWords)
</code></pre>

<p>Im getting the error:</p>

<p>Collection was modified; enumeration operation may not execute</p>

<pre><code>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&amp; m)
       at System.Windows.Forms.Control.ProcessKeyMessage(Message&amp; m)
       at System.Windows.Forms.Control.WmKeyChar(Message&amp; m)
       at System.Windows.Forms.Control.WndProc(Message&amp; m)
       at System.Windows.Forms.ListBox.WndProc(Message&amp; m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; 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: 
</code></pre>
