4

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

我有 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)时才会发生这种情况。Form1,Form2 效果很好,。但是如果我查看 form3 并尝试返回 form1 那么在 form3 的关闭事件中某处,form1

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

谢谢

4

2 回答 2

5

只需调用 Environment.Exit(0);

于 2016-05-19T14:26:31.983 回答
4

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

    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();
    }

先调用this.Close()然后再调用Application.Exit(),Application.Exit()终止所有进程,Close()关闭你的主窗体

于 2013-08-06T11:04:28.410 回答