c#集合被修改枚举操作可能无法执行 [英] c# collection was modified enumeration operation may not execute

查看:55
本文介绍了c#集合被修改枚举操作可能无法执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个项目程序,并试图检测文本框cahnges befor close form,我收到以下错误:C#Collection被修改;枚举操作可能无法执行。以下是我所指的代码:

I am create an project program and trying to detect textbox cahnges befor closing form,i am getting the following error: C# Collection was modified; enumeration operation may not execute. Below is the code I am referring to:

 public partial class PatientFiles : Form, ILookup
{
 bool NeedSaving = false;
//i think my error here but how can fix this
 void CheckChanges(Control.ControlCollection cc)
{
    {
     try{
            foreach (Control ctrl in cc)
            {
                MaskedTextBox mtxtBox = ctrl as MaskedTextBox;
                if (mtxtBox != null)
                {
                    mtxtBox.TextChanged +=TextWasChanged;
                }
                else
                {
                    TextBox txtBox = ctrl as TextBox;
                    if (txtBox != null)
                    {
                        txtBox.TextChanged += TextWasChanged;
                    }
                    else
                    {
                        ComboBox cmb = ctrl as ComboBox;
                        if (cmb != null)
                        {
                            cmb.SelectedIndexChanged +=TextWasChanged;
                        }
                    }
                }
                CheckChanges(ctrl.Controls);
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }       
//formload
  private void frmPatient_Load(object sender, EventArgs e)
{
   EnableNavigation();
   //txtEngName.TextChanged += new EventHandler(TextWasChanged);
   CheckChanges(this.Controls);

   }` 

   public void TextWasChanged(object sender, EventArgs e)
  {
   NeedSaving = true;
  }`

 private void PatientFiles_FormClosing(object sender,    FormClosingEventArgs e)
  {
   //NeedSaving();
   // Disable Navigation On Form closing
   if (NeedSaving)
   {

       DialogResult dt = MessageBox.Show("Save Changes", "information", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
       if (dt == DialogResult.Yes)
       {
           SaveData();
           //DisableNavigation();
       }

       else if (dt == DialogResult.No)
       {

           DisableNavigation();
           NeedSaving = false;
           this.Close();
       }
       else if (dt == DialogResult.Cancel)
           e.Cancel = true;
   }
 }
}

推荐答案

一般规则是:不要修改集合在枚举期间(循环遍历此集合),不要添加或删除元素。从你的代码中,我看不到你在哪里做,所以也许你的观察不正确,问题出在其他地方。在调试器下运行它以查看它发生的行。您可以随时解决所有问题而无需修改枚举的集合。



顺便说一下,不要在本地处理异常。对异常处理没有任何特殊处理,只显示消息。这样的片段应该只是整个线程中的一个。使用UI线程,它在主事件周期中完成,UI库为此目的有特殊事件。可能你正在使用 System.Windows.Forms ,那么你可以使用 Application.ThreadException ,如我过去的回答所示:捕获例外 [ ^ ]。



好运气。

-SA
The general rule is: don't modify a collection during enumeration (loop traversing this collection), don't add or remove elements. From your code, I cannot see where you do it, so maybe your observations are not correct, the problem is somewhere else. Run it under the debugger to see in what line it happens. You can always solve all problems without modification of the collection being enumerated.

By the way, don't handle exception locally. You do nothing special with exception handling, just show the message. Such fragment should be only one in the whole thread. With UI threads, it's done in a main event cycle, UI libraries have special event for this purpose. Probably you are using System.Windows.Forms, then you can use Application.ThreadException, as shown in my past answer: Catching an Exception[^].

Good luck.
—SA


这篇关于c#集合被修改枚举操作可能无法执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆