我的程序有问题。
我有3种形式:第一种形式打开第二种形式。第二个打开第三个表格或返回第一个表格。第三窗体可以打开第一或第二窗体。
这是我打开第二个表单的方式:
private void Open_second_form()
{
Form2 myForm = new Form2(Type_person);
this.Hide();
myForm.ShowDialog();
this.Close();
}
我打开的其余表格完全相同。
这是我如何关闭表格的代码。每个表单都有此方法:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Exit or no?",
"My First Application",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information) == DialogResult.No)
{
this.Close();
Environment.Exit(1);
}
}
当我打开第三个表单时,我得到3个MessagesBoxes。如果我打开第一个表单,则只有1个MessageBox。
我想关闭所有窗体,而只获取一个MessageBox。
我尝试了很多解决方案,但没有一个奏效。我试过了Application.exit();
请帮我 :)
您的确认消息很有趣,结果不明显= D
有两种解决方案可以解决您的问题。
1)如果用户选择关闭应用程序-不再显示确认信息
private static bool _exiting;
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (!_exiting && MessageBox.Show("Are you sure want to exit?",
"My First Application",
MessageBoxButtons.OkCancel,
MessageBoxIcon.Information) == DialogResult.Ok)
{
_exiting = true;
// this.Close(); // you don't need that, it's already closing
Environment.Exit(1);
}
}
2)用于CloseReason
确认仅用户操作
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
if(MessageBox.Show("Are you sure want to exit?",
"My First Application",
MessageBoxButtons.OkCancel,
MessageBoxIcon.Information) == DialogResult.Ok)
Environment.Exit(1);
else
e.Cancel = true; // to don't close form is user change his mind
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句