How do I catch the event of exiting a Winforms app

2020-08-15 04:07发布

问题:

If the user wants to exit the application by clicking on the exit icon or by ALT+F4, I'd like to make a dialog box questioning the user if he/she is really sure about exiting.

How do I capture this event before the application is actually closed?

回答1:

Check out the OnClosing event for the form.

Here is an extract from that link actually checks for a change on a text field and prompts a save:

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
   // Determine if text has changed in the textbox by comparing to original text.
   if (textBox1.Text != strMyOriginalText)
   {
      // Display a MsgBox asking the user to save changes or abort.
      if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
         MessageBoxButtons.YesNo) ==  DialogResult.Yes)
      {
         // Cancel the Closing event from closing the form.
         e.Cancel = true;
         // Call method to save file...
      }
   }
}

You can change the text to suit your needs, and then also I think you might want to switch the DialogResult.Yes to DialogResult.No based on your text.


Here is some modified code just for you:

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
   if(MessageBox.Show("Are you sure you want to quit?", "My Application", MessageBoxButtons.YesNo) ==  DialogResult.No)
   {
      e.Cancel = true;
   }
}


回答2:

You should subscribe to the Form_Closing event
Post a dialog box there, and if user abort the closing set the FormCloseEventArgs.Cancel to true.

For example in the Form_Load or using the designer, subscribe the event

Form1.FormClosing += new FormClosingEventHandler(Form1_Closing);

....
private void Form1_FormClosing(Object sender, FormClosingEventArgs e) 
{
    DialogResult d = MessageBox.Show("Confirm closing", "AppTitle", MessageBoxButtons.YesNo );
    if(d == DialogResult.No)
        e.Cancel = true;
}

Depending on the situation is not always a good thing to annoy the user with this kind of processing.
If you have valuable modified data and don't want the risk to loose the changes, then it is always a good thing to do, but if you use this only as a confirm of the close action then it's better to do nothing.



回答3:

You can handle the Form_Closing or Form_Closed events for this.

In Visual Studio click the lightning bolt icon and scroll down to these events in the form properties list. Double click on the one you want and it will hook up the event for you.



回答4:

Read msdn sample: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing.aspx



回答5:

Is this just one form? If so, you might want to use the FormClosing event, which allows you to cancel it (show the dialog, then set CancelEventArgs.Cancel to true if the user chooses to cancel closing).



回答6:

If you're talking about windows forms, should be enough to catch your MainWindow's

FormClosing event and in case you want prevent closing, just set the argument of the event handler got fired, to true.

Example:

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{

      if(MessageBox.Show("Do you really want to exit?", "My Application",
         MessageBoxButtons.YesNo) ==  DialogResult.No){

                // SET TO TRUE, SO CLOSING OF THE MAIN FORM, 
                // SO THE APP ITSELF IS BLOCKED
                e.Cancel = true;            
      }
   }
}


标签: c# .net winforms