如何显示一个MessageBox提示,当用户点击了跨在标题栏中 [英] How do I show a MessageBox prompt when the user has clicked the cross in the title bar
问题描述
我目前正在开发一个C#Windows窗体应用程序。
I am currently developing a C# Windows Form Application.
用户通过登录表单登录后,它将被带到MainForm的。
After the user has login through the loginForm, it will be brought to the mainForm.
我想它的方式代码,用户点击之后在MainForm的标题栏在十字架上,会有一个提示,询问用户这将关闭应用程序。确认?
其次是yes和no按钮。
I would like to code it in a way that after the user click the cross on the title bar in the mainForm, there would be a prompt asking the user "This will close the application. Confirm?" followed by a yes and no button.
如果是,将显示另一个框,应用程序关闭!
If yes, another box will be displayed, "Application Closed!"
如果没有,在MessageBox只会关闭,应用程序将继续运行。
If no, the messagebox will just close and the application will continue running.
我当前的代码是:
private void mainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("This will close down the whole application. Confirm?", "Close Application", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
MessageBox.Show("The application has been closed successfully.", "Application Closed!", MessageBoxButtons.OK);
System.Windows.Forms.Application.Exit();
}
else
{
this.Activate();
}
}
但它不能正常工作。
however it does not work.
首先,我不知道为什么在MessageBox为了使整个事情关闭两次弹出。
Firstly I have no idea why the messagebox pops up twice in order for the whole thing to close.
其次,窗体关闭为好,我不能把它带回来。
Secondly if i click no, the form closes as well and I am not able to bring it back.
推荐答案
要取消形式的关闭,在你else语句需要 e.Cancel = TRUE;
。
你不需要在你的真实情况下的明确的退出。
To cancel the closing of the form, in your else statement you need e.Cancel = true;
.
You don't need the explicit Exit in your true case.
试试这个
private void mainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("This will close down the whole application. Confirm?", "Close Application", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
MessageBox.Show("The application has been closed successfully.", "Application Closed!", MessageBoxButtons.OK);
}
else
{
e.Cancel = true;
this.Activate();
}
}
我假设你的第二个MessageBox中说,它已关闭,如果仅用于测试目的。
I assume your second MessageBox to say it has closed if for testing purposes only.
您可能只需要。
private void mainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("This will close down the whole application. Confirm?", "Close Application", MessageBoxButtons.YesNo) != DialogResult.Yes)
{
e.Cancel = true;
}
}
注意语句检查,看看如果他们不打是,而不是,如果他们打取消。这意味着,如果他们打的X对话框上它不会被算作确认
Notice the statement checks to see if they didn't hit yes, rather than if they hit cancel. This means that if they hit the x on the dialog box it won't be counted as a confirmation.
编辑:如果MainForm中不是主要形式
好吧,我想我已经得到了你现在问。
Okay, I think I've got what you're asking now.
什么我会做的就是把我的代码在我的第二个代码块以上的的FormClosing,然后在FormClosed事件处理程序有这个
What I would do is put the code I have in my second code block above in the FormClosing, and then in the FormClosed event handler have this
private void mainForm_FormClosed(Object sender, FormClosedEventArgs e)
{
MessageBox.Show("The application has been closed successfully.", "Application Closed!", MessageBoxButtons.OK);
System.Windows.Forms.Application.Exit();
}
这篇关于如何显示一个MessageBox提示,当用户点击了跨在标题栏中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!