Shi*_*eek 2 c# forms windows dialog winforms
我在关闭Windows窗体申请表的事件时使用以下代码:
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr = MessageBox.Show("Are you sure you want to quit?", "Leaving App",MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.No)
{
return;
}
else
Application.Exit();
}
Run Code Online (Sandbox Code Playgroud)
但问题是,每当用户单击"是"时,应用程序结束,但当用户单击"否"时,应用程序仍在运行,但表单隐藏.如果用户单击"否",如何保持表单可见?
更新
我面临的另一个问题是,当我单击是时,MessageBox再次显示,然后我单击是应用程序退出.为什么它显示两次?
你不必打电话,Application.Exit()因为如果你不取消关闭它,程序将退出自己.以下是正在运行的更正代码:
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr = MessageBox.Show("Are you sure you want to quit?", "Leaving App",MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.No)
{
e.Cancel = true;
}
}
Run Code Online (Sandbox Code Playgroud)
private void Form1_Closing(Object sender, CancelEventArgs e) {
if (!isDataSaved) {
e.Cancel = true;
MessageBox.Show("You must save first.");
}
else {
e.Cancel = false;
MessageBox.Show("Goodbye.");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8567 次 |
| 最近记录: |