2013-02-24 80 views
4

我寫了下面的代碼,它從列表框中刪除選定的值。我也想從字典列表中刪除它,然後將其更新/寫入文本文件,所以當我再次運行該程序並加載文本文件時,它將被更新,如果沒有,它會繼續顯示刪除的項目,每次我運行我的再次申請。爲什麼我得到一個異常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: 
+0

你的兩個for循環沒有意義。您循環訪問字典的鍵,然後遍歷列表中的項並嘗試爲列表中的每個元素刪除字典的鍵。這裏有點不對勁。 – Steve 2013-02-24 23:11:48

回答

8

您從LocalyKeyWords而在遍歷它的中間刪除項目;這是不允許的,正如例外信息所述。

我不確定這裏的大圖是什麼,但是本地化的解決方案是製作LocalyKeyWords的臨時副本並對其進行迭代。然後,您可以毫無困難地修改「源」集合。

例子:

foreach (var kvp in LocalyKeyWords.ToList()) // .ToList() makes a temp copy 
1

不能修改的foreach內的IEnumerable集合。

您可以創建字典的副本,並通過修改原來的字典來循環。

相關問題