winform intercepts an example of an event triggered by the close button

  • 2020-06-12 10:29:52
  • OfStack

When the user turns off the software, software 1 will generally give a "confirm to close" prompt. Normally, we write it in an FormClosing event, and if it's closed, it's closed; Otherwise, setting the Cancel property of FormClosingEventArgs to true cancels the closing of the form.

If the form is the main form and we want to close the entire application when the form closes, we have at least two situations:

(1) The form is also a startup form, meaning it is the parent of all forms in the application, and the entire application is closed.

(2) If the form is not a launch form, as if we made a welcome form, then the parent class of all forms in the application is the welcome form. Closing the form does not close the entire application. We need to add another line of "Application.Exit ()" to exit the application. The closing of the parent form sends the closing event to the form, so the event is called once more and the confirmation dialog pops up a second time. The solution is the following code:

Note: The welcome screen can be opened in the main form by the ShowDialog method instead of using the welcome screen as the launch form. The following method is of only theoretical significance.


 protected override void WndProc(ref Message m)
{
    //Console.WriteLine(m.Msg);
    const int WM_SYSCOMMAND = 0x0112;
    const int SC_CLOSE = 0xF060;
    if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
    {
        // Capture the closing form message 
        // Click the close Form control button     Comment for minimize form    
        //this.WindowState = FormWindowState.Minimized;

        // Form hidden 
        this.Hide();
        return;
    }
    base.WndProc(ref m);
}

C# events that can be triggered when the form closes

FormClosing: The FormClosing event occurs when the form closes. When the form closes, this event is processed, freeing all resources associated with the form. If you cancel this event, the form remains open. To unclose a form, set the Cancel property of FormClosingEventArgs passed to the event handler to true.

FormClosed: An FormClosed event occurs after the user or the Close method of the Application class or Exit method closes the form. To prevent the form from closing, process the FormClosing event and set the Cancel property of CancelEventArgs passed to the event handler to true. You can use this event to perform tasks such as releasing resources used by the form, save information in the input form, or update its parent form.

When the form displays as a modal dialog box, clicking the Close button (the X button in the upper right corner of the form) hides the form and sets the DialogResult property to ES44en.Cancel. By setting the DialogResult property in the event handler of the FormClosing event on the form, you can override the value assigned to the DialogResult property when the user clicks the Close button.

Pay attention to

When the Close method is called on Form displayed as a schemaless window, the Show method cannot be called to make the form visible because the form's resources have been freed. To hide the form and then make it visible again, use the Hide method.

If the form is a multi-document interface (MDI) parent form, the FormClosing event for all MDI child forms is raised before the FormClosing event for the MDI parent form. Similarly, FormClosed events for all MDI child forms are raised before FormClosed events for the MDI parent form are raised. Cancelling the FormClosing event on the MDI child form does not prevent raising the FormClosing event on the MDI parent form. However, canceling this event sets the Cancel property of the FormClosingEventArgs class passed as a parameter to the parent form to false. To force all MDI parent and child forms to close, set the Cancel property on the MDI parent form to false.

Here's a simple example:


 private void Form2_FormClosing(object sender, FormClosingEventArgs e)  
 {  
     DialogResult result = MessageBox.Show(" Are you sure you want to close it! ", " Prompt information ", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);  
     if (result == DialogResult.OK)  
     {  
e.Cancel = false;  // Click on the OK
     }  
     else 
     {  
e.Cancel = true;  
     }  
 }


Related articles: