我收到此错误集合已修改;枚举操作可能无法执行。
我有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();
}
Run Code Online (Sandbox Code Playgroud)
表格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();
}
Run Code Online (Sandbox Code Playgroud)
表格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;
}
}
Run Code Online (Sandbox Code Playgroud)
它仅在我查看第三个表单(Form3)时发生。Form1、Form2 运行良好,。但是如果我查看 form3 并尝试返回 form1 所以在 form3 的关闭事件中的某个地方,form1
我的猜测是btnExit_close表单的事件this.close()
谢谢
关闭第一个表单时,请在 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();
}
Run Code Online (Sandbox Code Playgroud)
先调用this.Close(),然后调用Application.Exit(),Application.Exit()终止所有进程,Close()关闭主窗体