关闭表格时提示? [英] Prompt when closing a form?

查看:95
本文介绍了关闭表格时提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现的是,当用户点击红色退出按钮时,将首先出现提示,确保用户确实想要退出程序而不是仅仅意外地单击退出按钮。



我有这个代码:



What I'm trying to achieve is that when the user clicks on the red exit button, a prompt will appear first, making sure that the user really wants to exit the program and did not just accidentally click on the exit button.

I have this code:

private void FormA_FormClosing(object sender, FormClosingEventArgs e)
        {


            DialogResult closing = MessageBox.Show("Are you sure you want to exit the game?", "EXIT", MessageBoxButtons.YesNo);


            if (closing == DialogResult.Yes)
            {
                Application.Exit();
            }



        }





当出现提示时用户单击退出按钮,但即使您单击否按钮,它也会关闭程序!

当您单击是时,第二次出现提示,您必须单击是的再次退出申请。



顺便说一下,这不在主表格上。



我做错了什么?



And the prompt appears when the user clicks on the Exit button, but even if you click on the No button, it closes the program!
And when you click on Yes, the prompt appears for the second time and you have to click on Yes again to exit the application.

This isn't on the main form by the way.

What am I doing wrong?

推荐答案

你必须这样做:

You have to do it like this:
private void FormA_FormClosing(object sender, FormClosingEventArgs e)
{
    DialogResult closing = MessageBox.Show("Are you sure you want to exit the game?", "EXIT", MessageBoxButtons.YesNo);
   if (closing == DialogResult.No)
   {
       e.Cancel = true; // if you don't want to exit the game (if you pressed No), cancel the closing   
   }
}



您的代码无效,因为当您点击否时,没有任何内容可以取消结束。







如果您想要在您的表格(不是主表格)关闭时退出申请,那么您必须在子表单关闭后退出应用程序:


Your code didn't work because when you clicked No, nothing would cancel the closing.



If you want that the application exits when your form (which is not the main form) closes, then you have to exit the application after the child form is closed:

// when creating child form
ChildForm childFrm = new ChildForm();
childFrm.FormClosed += childFrm_FormClosed;

// in childFrm_FormClosed method:
private void childFrm_FormClosed(Object sender, FormClosedEventArgs e)
{
    Application.Exit(); // exit application
}



请注意,您必须在创建子表单的表单的类中添加 childFrm_FormClosed 。您也不必为每个子表单执行此操作,只需执行此操作。


Note that you have to add the childFrm_FormClosed in the class of the form that creates your child form. You also don't have to do this for every child form, only this one.


您需要设置
e.Cancel = true

停止表单关闭,因此代码变为:



to stop the form from closing, so the code becomes:

private void FormA_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason != CloseReason.ApplicationExitCall)
    {
      DialogResult closing = MessageBox.Show("Are you sure you want to exit the game?", "EXIT", MessageBoxButtons.YesNo);

      if (closing == DialogResult.Yes)
      {
          Application.Exit();
      }
      else
      {
          e.Cancel = true;
      }
    }
}





从技术上讲,你不需要别的,因为If.Exit中的Application.Exit调用无论如何都会停止执行,但我也总是试图提高可读性。



Technically, you don't need the else, because the Application.Exit call in the If branch will stop execution anyway, but I always try to go for readability as well.


这篇关于关闭表格时提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆