C # events triggered by the winform intercept and close button

Source: Internet
Author: User

When you close the software, the software usually prompts "are you sure you want to close it.

Generally, we write it in the formclosing event. If it is determined to close, it will be closed; otherwise, the cancel attribute of formclosingeventargs will be set to true, and the close of the form will be canceled.

If the form is the main form, we want to close the entire application when the form is closed, there will be at least two situations:

(1) The form is also a startup form, that is, it is the parent class of All Forms in the application, the entire application will be closed.

(2) If the form is not a startup form, for example, if we make a welcome form, the parent class of All Forms in the application is the welcome form. The close of the form does not close the entire application. We need to add the "application. Exit ()" line of code to exit the application. But at the same time there is a problem, the closure of the parent form will send a closing event to the form, so the event is called again, and the confirmation dialog box will pop up for the second time. The solution is to use the following code:

Note: You can use the showdialog method in the main form to open the welcome interface instead of using the welcome interface as the startup form. The following method has 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 closed form message // the user clicks the close form control button to comment to minimize the form // This. windowstate = formwindowstate. minimized; // hide this. hide (); return;} base. wndproc (ref m );}

**************************************** **************************************** *******************************

C # events that can be triggered when the form is closed

Formclosing: when the form is closed, the formclosing event occurs. When the form is closed, this event is processed to release all resources associated with the form. If you cancel this event, the form remains open. To cancel the form close operation, set the cancel attribute passed to the formclosingeventargs event handler to true.

Formclosed: A formclosed event occurs when the close method or exit method of the user or application class closes the form. To prevent the form from being closed, handle the formclosing event and set the cancel attribute passed to the event handler to true. You can use this event to execute some tasks, such as releasing resources used by the form. You can also use this event to save information in the input form or update its parent form.

When the form is displayed as a mode dialog box, click the close button (the button with X in the upper right corner of the form) to hide the form and set the dialogresult attribute to dialogresult. Cancel. By setting the dialogresult attribute in the event handler of the formclosing event in the form, you can override the value assigned to the dialogresult attribute when you click the close button.

Note:
When you call the close method on a form that is displayed as a mode-free window, you cannot call the show method to make the form visible because the form resources have been released. To hide a form and make it visible, use the hide method.
 

If the form is a multi-Document Interface (MDI) parent form, the formclosing event of all MDI child forms will be triggered before the formclosing event of the MDI parent form is triggered. Similarly, before the formclosed event of the MDI parent form is triggered, all the formclosed events of the MDI child form are triggered. Canceling the formclosing event of the MDI child form cannot prevent the formclosing event of the MDI parent form from being triggered. However, canceling this event sets the cancel attribute of the formclosingeventargs class passed as a parameter to false for the parent form. To forcibly close all MDI parent forms and child forms, set the cancel attribute in the MDI parent form to false.

The following is a simple example:

Private void form2_formclosing (Object sender, formclosingeventargs e) {dialogresult result = MessageBox. Show ("are you sure you want to disable it! "," Message ", messageboxbuttons. okcancel, messageboxicon. information); If (result = dialogresult. OK) {e. cancel = false; // click OK} else {e. cancel = true ;}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.