当用户单击标题栏中的叉号时,如何显示MessageBox提示?
本文关键字:何显示 显示 提示 MessageBox 标题栏 单击 用户 | 更新日期: 2023-09-27 18:12:57
我目前正在开发一个c# Windows窗体应用程序。
用户通过loginForm登录后,它将被带到mainForm。
我想以一种方式编码它,在用户单击mainForm标题栏上的十字之后,会有一个提示询问用户"这将关闭应用程序。"确认?"然后是一个是和否按钮。
如果是,将显示另一个框,"Application Closed!"
如果没有,消息框将关闭,应用程序将继续运行。
我当前的代码是:
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();
}
}
但是它不工作。
首先,我不知道为什么消息框弹出两次,以便整个东西关闭。
其次,如果我点击no,表单也关闭了,我不能把它带回来。
要取消表单的关闭,在else语句中需要e.Cancel = true;
。在真实情况下,不需要显式的Exit。
试一试
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();
}
}
我假设你的第二个消息框说它已经关闭,如果只是为了测试的目的。
你可能只想。
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;
}
}
注意语句检查它们是否没有点击yes,而不是点击cancel。这意味着,如果他们点击对话框上的x,它将不会被视为确认。
EDIT:如果mainForm不是主表单
好了,我想我现在明白你的意思了。
我要做的是把我在上面的第二个代码块中的代码放在FormClosing中,然后在FormClosed事件处理程序中加入这个
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();
}
这应该对你有帮助
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Are you sure you want to close?", "Close", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
e.Cancel = true;
}
要获得所需的功能,请尝试以下代码:
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);
//no need to do anything here.
}
else
{
e.Cancel = true;
}
}
您应该查看FormClosingEventArgs
中的Cancel属性:这应该可以工作:
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;
}
}
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.aspx 这将满足您的需求:
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;
}
}
编辑:回答你的第一个问题,见这个。当您在应用程序中调用Exit()
时,将为所有打开的表单引发一个FormClosing
事件,包括mainForm
(仍然打开),因此该事件将被引发两次。通常情况下,我认为这样的事情会导致无限递归和文字堆栈溢出,但我认为。net足够聪明,可以防止这种情况发生。
Exit()
来关闭你的程序-这是一种代码气味。
var GetInput = MessageBox.Show("Do you want to Delete?", "Confirm Deletion",MessageBoxButton.OKCancel);
if (GetInput == MessageBoxResult.OK)
{
//do what you like
}
你的代码对我来说是完美的,没有错误。
private void exiteProToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("message", "title", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Application.Exit();
}
else
{
this.Activate();
}
}