关闭表单按钮事件 [英] Close Form Button Event

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

问题描述

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

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.

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

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

在注销项下条码是:

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

在退出项下的条码是:

 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

推荐答案

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

This should handle cases of clicking on [x] or 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;
   }
}   

这篇关于关闭表单按钮事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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