locked
Application.Exit RRS feed

  • Question

  • Ok. Can someone tell me how to when the application closes to perform a function..maybe something like this.

    if (application.exit == true)

    {

    MessageBox.Show("Are you sure");

    }

    Friday, January 13, 2006 3:18 PM

Answers

  • Take a look at the Application.ApplicationExit event. You cannot cancel the exit because the Application.Exit will kill your forms without raising the Closing event.

    If you need to have the ability to cancel, then do NOT call Application.Exit. Instead call the Form.Close method of the primary form within your application(the one you passed to Application.Run). You can then handle the Form.Closing event and use the CancelEventArgs to cancel the close.

    Friday, January 13, 2006 4:17 PM

All replies

  • Take a look at the Application.ApplicationExit event. You cannot cancel the exit because the Application.Exit will kill your forms without raising the Closing event.

    If you need to have the ability to cancel, then do NOT call Application.Exit. Instead call the Form.Close method of the primary form within your application(the one you passed to Application.Run). You can then handle the Form.Closing event and use the CancelEventArgs to cancel the close.

    Friday, January 13, 2006 4:17 PM
  • Use the FormClosing event.
    Friday, January 13, 2006 8:18 PM
  • private void Form1_FormClosing(object sender, FormClosingEventArgs e)

    {

    e.Cancel = true;

    }

    i have the same problem

    this function does not work when the user click the X (top-right corner button)

    anayone with a suggestion about stop closing the application when the user presses the X button

    thanks

    Thursday, February 16, 2006 10:03 PM
  • private void OnFormClosing(object sender, FormClosingEventArgs e)

    {

    e.Cancel = DialogResult.No == MessageBox.Show("Close ?", "", MessageBoxButtons.YesNo);

    }

    Works for me here.

     

    Friday, February 17, 2006 1:20 AM