简体   繁体   中英

How do I add code to the exit button in a c# form?

I'm using Microsoft Visual C# 2008 Express.

In my main form, there's that X button to close the form at the top right. How do I add code to this button? In my menu, I have an "Exit" item and it has code that cleans up and closes my databases. How do I add the same code to this button if the user chooses that as a way to exit?

Thanks!

-Adeena

使用FormClosing事件应该捕获关闭表单的任何方式。

In the Design view for your form, within the Properties window, select the Events button and scroll down to the "FormClosed" and "FormClosing" events.

FormClosed is called after the form is closed.

FormClosing is called before the form is closed and also allows you to cancel the close, keeping the form open:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
}

If you want to ask the user "Are you sure you want do close this form?", then use FormClosing , where you can set Cancel = True and the form would remain open.

If you want to close some resource only when the form is definitely closed, then you use FormClosed event.

If you are in control of the whole code, then it kind of does not matter. But what you do not want to happen is to clean-up the resources using FormClosing when the the other handler of the event will keep the form open.

双击form'design中的退出按钮,然后简单地调用Dispose()方法

Form action method //

 protected override void OnFormClosing(FormClosingEventArgs e)

          {
          base.OnFormClosing(e);

          if (e.CloseReason == CloseReason.WindowsShutDown) return;

               switch (MessageBox.Show(this, "Are you sure you want to exit?", "Exit", MessageBoxButtons.YesNo))
                     {
                       case DialogResult.No:
                           e.Cancel = true;
                           break;
                     default:
                          break;
                     }
         }
 You can use form closing events choose or set closing event in  properties window      
 You can add dialog conditions based on what task you want to perform before closing form

private void Form1_FormClosing(object sender,FormClosingEventArgs e)
{
Application.Exit();
//you can also use Application.ExitThread();
}

使用Winform的Closed-Event。

FormClosing/FormClosed lets you watch the event for that form which may coincide with the application exiting. However, there is another event you can wire up called Application.ApplicationExit.

In your Main method:

Application.ApplicationExit += Application_ApplicationExit;

...

private static void Application_ApplicationExit(object sender, EventArgs e) {

  // do stuff when the application is truly exiting, regardless of the reason

}

This code will capture the user clicking on the 'X' or using Alt-F4 on a form to allow you to do something. I had to use this because the I needed the action to call my closing event, and it wouldn't call it when using the FormClosing Event due to racing events.

/// <summary>
/// This code captures the 'Alt-F4' and the click to the 'X' on the ControlBox
/// and forces it to call MyClose() instead of Application.Exit() as it would have.
/// This fixes issues where the threads will stay alive after the application exits.
/// </summary>
public const int SC_CLOSE = 0xF060;
public const int WM_SYSCOMMAND = 0x0112;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
    if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
        MyClose();

    base.WndProc(ref m);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM