In the thread .NET / Windows Forms: remember windows size and location, multiple solutions for saving the size and location of the form are given. However, all of them suggests saving the information in the FormClosing
event handler. I'm curious: is there any problem with saving the information in FormClosed
? It seems more natural to me, because that is the event where we are certain the form is being closed. When I tested, both events seem to work fine.

- 1,503
- 9
- 19
-
You'll get away with it, even though the actual window is no longer there. A lot of basic properties (Location, Size, WindowState) are cached in private fields of the Form class. So the property getter doesn't have to make a winapi call to obtain them and they have the last-known value. Sensible, well, somebody is going to read that code some day and lose ten minutes of his life wondering how it can be right. It is up to you. – Hans Passant May 24 '17 at 01:26
-
@HansPassant So the point is that those properties are still available, not because they still logically exist, but because they are cached. I guess I understand now. – Imperishable Night May 24 '17 at 01:30
1 Answers
It seems more natural to me, because that is the event where we are certain the form is being closed.
FormClosing
occurs just before FormClosed
event. Unless you cancel it in the FormClosing
event, you can still be certain that it is going to be closed.
You can use this event to perform tasks such as freeing resources used by the form and to save information entered in the form or to update its parent form.
However, note that whenever you close your program's startup form, the entire application should exit automatically.
As you have noticed, it will work for most of the time. But you might experience some weird behaviors (not saving settings) time to time.
When it comes to saving the size and location, there are more tricky things to consider, especially when you have multiple monitors with different resolutions/size and etc. So, I would eliminate one possible risk. So, unless there is any good reason to use FormClosed
, I would use FormClosing
for this purpose.

- 46,289
- 20
- 116
- 131
-
Does that mean there is a possibility that FormClosed does not happen at all? – Imperishable Night May 24 '17 at 01:32
-
@ImperishableNight: No, it will for sure. But if application start exiting before you save your file handlers, there might be some issues... – CharithJ May 24 '17 at 01:34
-
Also, I can imagine that in the future, to extend the functionality of my form, I add another handler to the FormClosing event, which may cancel the closing. Of course there is no harm in saving the size and location even if the form is not actually closed... I guess that is why FormClosing is the safer option. – Imperishable Night May 24 '17 at 01:35