C # Close and Closing, onclosing and closed

Source: Internet
Author: User

There are many software that can be set to "close the main panel when minimized to the system tray, without exiting the program". is done by overloading the onclosing of the program itself. So what is onclosing?

Degree Niang a bit, found that the problem involves not only closing itself. And close, Closed, Closing .... The following post-sharing knowledge:

The first is Form.close, and Close is a method that the form can call to shut itself down.

then onclosing, we can implement some sort of processing before the form closes by overloading the onclosing. For example, to minimize a form by overloading onclosing. Code:

1 protected Override void onclosing (Caneleventargs e) 2 {3     true ; 4     this. WindowState = formwindowstate.minimized; 5     Hide (); 6 }

The Form.close method is probably like this:

1 form.close () 2 {3      OnClosing (this. e); 4       if (!e.cancel) // The default is False, which is true 5       // Close Window 6 }

Therefore, calling the OnClosing function without the close function will close the window as usual. Because you directly call the OnClosing method, the Close () method is not called at all.

Thus, when we click the fork in the upper-right corner of the window, the form first calls the Close method, and the Close method calls the OnClosing method. By overloading the OnClosing method, we let the program not exit, but minimize it to the system tray area.

Form.close raises onclosing,onclosing closing and formclosing.

However, on MSDN, the Form is declared in the. NET Framework version 2.0 and later. closed and form.closing events are obsolete, use Form instead. Formcloseing and Form. formclosed event. The introduction of closing here is just to expand the knowledge.

Then there is the form.formclosing, the voice is in progress. The thing that needs to be handled when the form is closed is basically the same as the OnClosing method in front. For example, C # can add the NotifyIcon control, adding the ICO icon. Set the ShowInTaskbar in the form property to False.

Then write the Form.formclosing event for the form. Can get:

1 Private void Form1_formclosing (object  sender, FormClosingEventArgs e)2{3       True; // The default is false to close the form. True to not close.  4this      . WindowState = formwindowstate.minimized; // The form is minimized.  5     Hide ();   6 }

Each time the form is closed, it is automatically minimized. And because the Showinstaskbar is set, it will not appear in the taskbar. But because of the NotifyIcon, you can see your program in the system tray.

This is explained on MSDN:

1), forms when the form is closed.The FormClosing event occurred.When the form is closed, this event is processed to free all resources associated with this form.If this event is canceled, the form remains open.To cancel the closing of a form, pass the FormClosingEventArgs that is passed to the event handlerthe Cancel property is set to true. when you call the Close method on a form that is displayed as a modeless window, you cannot invoke the Show method to make the form visible because the form's resources have been disposed. to hide the form and then make it visible, use the hide method.

2), if the form is a multiple-document interface (MDI) parent form, the MDI parent form is raisedAll MDI child forms are raised before the FormClosing event.FormClosing event.Similarly, when an MDI parent form is raisedBefore the formclosed event, all MDI child forms are raisedFormClosed event.Cancels the MDI child form'sThe FormClosing event does not prevent the MDI parent form from being raised formclosing  event.  true the cancel property Of the formclosingeventargs class that's passed as a parameter to the parent fo Rm. " However, canceling the event is passed as a parameter to the parent form's   formclosingeventargs  class   cancel  property is set to   true.  cancel property to false in the MDI parent form. > to force all MDI parent forms and subforms to close, set the   cancel  property in the MDI parent form to   false.

Plainly, form.formclosing and onclosing exist to let us have the means to prevent the window from closing.

Finally, the Form.closed method:

The FormClosed event occurs after the Close method or the exit method of the user or application class closes the form. You can use this event to release some resources for the form. You can also use this event to save some information from the input form or to update the parent form. You can save the form, save the file, save the resources, and so on.

Reprint Please specify blog garden: http://www.cnblogs.com/gu-zhan/old Salt produced

C # Close and Closing, onclosing and closed

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.