Form close event

Source: Internet
Author: User

For developers who focus on processing events in the Windows Forms Application in order, the order of events is particularly important.

When a Windows form application is started, the startup events of the main form are triggered in the following order:

1. system. Windows. Forms. Control. handlecreated

Occurred when creating a handle for the control

2. system. Windows. Forms. Control. bindingcontextchanged

Occurs when the value of the bindingcontext attribute is changed.

Note: Each windows form must have at least one bindingcontext object. For details, see msdn.

3. system. Windows. Forms. Form. Load

Occurs before the first display of the form

4. system. Windows. Forms. Control. visiblechanged

Occurs when the visible attribute value is changed.

Note: This event is triggered if the visible attribute is modified programmatically or interactively.

5. system. Windows. Forms. Form. Activated

Occurs when code is activated or the user activates the form.

6. system. Windows. Forms. Form. shown

The shown event is triggered only when the form is displayed for the first time. Subsequent operations such as minimization, maximization, restoration, hiding, display, or invalidation, and re-drawing, do not cause the event.

 

 

When an application is closed, the closing event of the main form is triggered in the following order:

1. system. Windows. Forms. Form. Closing

Occurs when the form is closed.

Note: In. NET Framework 2.0, the closing event is out of date. You should use the formclosing event instead.

2. system. Windows. Forms. Form. formclosing

When the form is closed, you can cancel it.

3. system. Windows. Forms. Form. Closed

When the form is closed

Note: In. NET Framework 2.0, the closed event is out of date. You should use the formclosed event instead.

4. system. Windows. Forms. Form. formclosed

When the form is closed

5. system. Windows. Forms. Form. Deactivate

Occurs when the form loses focus and is no longer an active form.

Note: When the application. Exit method is called to exit the application, the form. Closed and form. Closing events are not triggered, but the form. formclosed and form. formclosing events are triggered.

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:

After the close method or exit method of the user or application class closes the form, the formclosed event occurs.

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.

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.

 

 

When the form is displayed as a mode-free dialog box, after close, the resources of the form have been released, and the show method cannot be called to make the form visible.

When the form is displayed as a modal dialog box, clicking the close button (the "X" button is in the upper-right corner of the form) hides the form and the dialogresult attribute is set to dialogresult. Cancel. When you click the close form button in the dialog box or set the dialogresult attribute value ,. net Framework does not call the close method. This form is hidden and can be re-displayed without creating a new instance in the dialog box. As the form displayed in the dialog box is hidden but not closed, the form is no longer needed by the application. You must call the form's dispose method.

In the following two cases, calling close does not release the form: (1) the form is part of the multi-Document Interface (MDI) Application and is invisible; (2) this form is a mode dialog box. In these cases, you need to manually call dispose to mark all controls of the form as required for garbage collection.

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.