Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is OnClosing obsolete and should I migrate to OnFormClosing

Tags:

c#

.net

winforms

I was reading the MSDN documentation for the System.Windows.Forms.Form.OnClosing() method and noticed:

CAUTION: The OnClosing method is obsolete in the .NET Framework version 2.0; use the OnFormClosing method instead.

I can find no reference as to why it had been marked as obsolete.
I figured I would ask here:

  1. Before I wasted hours drudging through reflector
  2. It would be useful to have as a reference to others

The only difference I see is that you get access to the FormClosingEventArgs, so if you don't need those (which you obviously didn't if you were using OnClosing) then is there any benefit to switching to OnFormClosing?

I understand that obsolete means that it could go away, so you should update as soon as possible, but you and I both know that obsolete in MS land means little. I want to know solid technical details as to why.

like image 622
Andrew Burns Avatar asked Jul 29 '09 16:07

Andrew Burns


1 Answers

I believe the main reason was to work around some of the inconsistencies with OnClosing, in particular, the fact that it is not called if the form is closing due to Application.Exit.

From the documentation:

The OnClosed and OnClosing methods are not called when the Application..::.Exit method is called to exit your application. If you have validation code in either of these methods that must be executed, you should call the Form..::.Close method for each open form individually before calling the Exit method.

The OnFormClosing provides access to the CloseReason member, which makes the handling much easier to handle correctly, in a clean, consistent manner.

like image 111
Reed Copsey Avatar answered Oct 09 '22 21:10

Reed Copsey