修改了应用程序出口集合;枚举操作可能无法执行



>我收到此错误 集合已修改;枚举操作可能无法执行。

我有3种表格。这些是所有 3 个的表单关闭事件,我做了一些研究,并了解到一些来自被修改/显示的依次导致此错误

表格1

private void btnExitl_Click(object sender, EventArgs e)
        {
            this.Close();   
        }
private void frmPlant_FormClosing(object sender, FormClosingEventArgs e)
    {
     if (DataDirty)
        {
            if (DialogResult.Yes == MessageBox.Show("Are you sure you want to exit", "Data Changed", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                 Application.Exit();
            else
                e.Cancel = true;
        }
        else
             Application.Exit();
    }

表格2:

 private void btnCancel_Click(object sender, EventArgs e)
    {
        this.Close();
    }
    private void frmInputFiles_FormClosing(object sender, FormClosingEventArgs e)
    {
        int plantid = StaticClass.GlobalValue;
        //Properties.Settings.Default.PlantId = plantid;
        Program.fPlant = new frmPlant(plantid);
        Program.fPlant.Show();
        e.Cancel = false;
        //this.Hide();
    }

表格3:

    private void btnClose_Click(object sender, EventArgs e)
    {
        this.Close();
    }
    private void frmVesselData_FormClosing(object sender, FormClosingEventArgs e)
    {

        DialogResult result;
        int fileId = StaticClass.FileGlobal;
        if (DataDirty)
        {
            string messageBoxText = "You have unsaved data. Do you want to save the changes and exit the form?";
            MessageBoxButtons button = MessageBoxButtons.YesNo;
            string caption = "Data Changed";
            MessageBoxIcon icon = MessageBoxIcon.Question;
            result = MessageBox.Show(messageBoxText, caption, button, icon);
            if (result == DialogResult.No)
            {
                Program.fInputFiles = new frmInputFiles(gPlantId, gPlantName);
                    Program.fInputFiles.Show();
                   //e.Cancel=true;
            }
            if (result == DialogResult.Yes)
            {
                e.Cancel = true;
                //return;
            }
        }
        else
        {
            Program.fInputFiles = new frmInputFiles(gPlantId, gPlantName);
                Program.fInputFiles.Show();
                //e.Cancel = false;
        }      
    }

仅当我查看第三种形式(Form3)时,才会发生这种情况。表格1,表格2工作良好,。但是如果我查看表单 3 并尝试返回表单 1 所以在表单 3、表单 1 的结束事件的某个地方

我的猜测是表单this.close()btnExit_close事件

谢谢

只需调用 Environment.Exit(0);

关闭您的第一个表单时,请尝试在 FormClosesingEvent 上执行此操作

    private void frmPlant_FormClosing(object sender, FormClosingEventArgs e)
    {
    if (DataDirty)
    {
        if (DialogResult.Yes == MessageBox.Show("Are you sure you want to exit", "Data Changed", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
        {
             this.Close();
             Application.Exit();
        }
        else
            e.Cancel = true;
    }
    else
         Application.Exit();
    }

叫这个。首先关闭(),然后关闭 Application.Exit(),Application.Exit() 终止所有进程,Close() 关闭主窗体

最新更新