C# 关闭表单按钮事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17796151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:30:04  来源:igfitidea点击:

Close Form Button Event

c#winforms

提问by Muhammed Salah

in my application, the user is first presented with the log in screen, and the form that shows up after you log in has a Menu Bar. On that menu bar are 2 items: "log out" and "exit". If the user selects the log out option, I want it to return to the aforementioned log in screen. If the user instead decided to click "exit", I prompt the user if they are sure they want to exit. Unfortunately, when the user decides to close the program by clicking the "X" button on the window, it closes only the current form. My intention would be for it to close the entire Application.

在我的应用程序中,用户首先看到登录屏幕,登录后显示的表单有一个菜单栏。在该菜单栏上有 2 个项目:“注销”和“退出”。如果用户选择注销选项,我希望它返回到上述登录屏幕。如果用户决定单击“退出”,我会提示用户是否确定要退出。不幸的是,当用户决定通过单击窗口上的“X”按钮关闭程序时,它只关闭当前窗体。我的意图是让它关闭整个应用程序。

Basically, I need to know how to exit the current application by intercepting the form closing event.

基本上,我需要知道如何通过拦截表单关闭事件来退出当前应用程序。

Under Logout item Strip Code Is :

在注销项下条码是:

 private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
 {
     Form_Login log = new Form_Login();
     this.Close();
     log.Show();
 }

Under The Exit item Strip Code Is :

在退出项下条码是:

 private void exitToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (MessageBox.Show("Are You Sure To Exit Programme ?","Exit",MessageBoxButtons.OKCancel)== DialogResult.OK)
     {
         Application.Exit();
     }
 }

And When I Click Exit Button It Close The Current Form and I Want To Close The Whole Application

当我点击退出按钮时,它会关闭当前表单,我想关闭整个应用程序

回答by JBelter

This should handle cases of clicking on [x] or ALT+F4

这应该处理点击 [x] 或 ALT+F4 的情况

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
   if (e.CloseReason == CloseReason.UserClosing)
   {
      DialogResult result = MessageBox.Show("Do you really want to exit?", "Dialog Title", MessageBoxButtons.YesNo);
      if (result == DialogResult.Yes)
      {
          Environment.Exit(0);
      }
      else 
      {
         e.Cancel = true;
      }
   }
   else
   {
      e.Cancel = true;
   }
}   

回答by Sriram Sakthivel

If am not wrong

如果我没有错

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
   //You may decide to prompt to user
   //else just kill
   Process.GetCurrentProcess().Kill();
} 

回答by Simon334512

Try this:

尝试这个:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    // You may decide to prompt to user else just kill.
    Process.GetCurrentProcess().Goose();
} 

回答by Er. Harry Singh

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();
    }  
}

回答by Udayakumar Thirupathi

Apply the below code where you want to make code to exit application.

在要退出应用程序的代码处应用以下代码。

System.Windows.Forms.Application.Exit( )

System.Windows.Forms.Application.Exit()

回答by hadi safari

Try This: Application.ExitThread();

试试这个:Application.ExitThread();