tags:

views:

1260

answers:

7

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

A: 

Use the Closed-Event of your winform.

Stephan Keller
is that "FormClosed" or "FormClosing"? or does it not matter if all I'm doing is things like closing databases...?
adeena
Sorry, typo: I meant the Closed-Event. But I just read that it is replaced by the FormClosed-Event from .NET 2.0 updwards
Stephan Keller
+9  A: 

Using the FormClosing Event should catch any way of closing the form.

Cookey
+4  A: 

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;
}
Matthew Brindley
A: 

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

}
Josh Einstein
A: 

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);
}
uzbones
A: 

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.

van
A: 

Hi! I'm Momin. Iwant to make a Exit Button in Access2007, Which is like that? "when I'll click on button close this from and open login from". Plz help me and give souution How can I make that?

Momin