我的应用程序在c#中关闭时出现问题。当我点击关闭按钮时,它会显示消息框的两倍或更多倍。我该怎么办?
private void home_FormClosed(object sender, FormClosedEventArgs e)
{
DialogResult dialog = MessageBox.Show("Are you sure you want to really exit ? ",
"Exit",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (dialog == DialogResult.Yes)
{
System.Windows.Forms.Application.Exit();
}
else if (dialog == DialogResult.No)
{
this.Show();
}
}
答案 0 :(得分:2)
使用Form1_FormClosing
事件,也不要像这样使用Application.Exit()
:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
var x = MessageBox.Show("Are you sure you want to really exit ? ", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (x == DialogResult.No)
{
e.Cancel = true;
}
else
{
e.Cancel = false;
}
}
或者像这样:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = MessageBox.Show("Are you sure you want to really exit ? ", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No;
}
答案 1 :(得分:2)
您应该使用Form.FormClosing
事件而不是FormClosed
事件。在参数中,您找到一个字段e.Cancel
。通过将此设置为false,您可以保持表单打开
答案 2 :(得分:0)
您可以通过检查<suite name="Suite1" verbose="1" parallel="methods" thread-count="5" preserve-order="false">
<test name="Login" >
<classes>
<class name="testSuite.TestSet1" />
</classes>
</test>
<test name="Product Search">
<classes>
<class name="testSuite.TestSet2"/>
</classes>
</test>
</suite>
事件中是否已调用Application.Exit()
来避免多个提示:
FormClosing
答案 3 :(得分:0)
改为使用表单结束事件。
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
var confirmation = MessageBox.Show("Sure to close form", "Confirm", MessageBoxButtons.YesNo);
if (confirmation == System.Windows.Forms.DialogResult.No)
{
e.Cancel = true; //Even cancelled, form will not get closed now
}
}