Lieven Cardoen
Lieven Cardoen

Reputation: 25959

Is there a reason why the FormClosing event of a WinForm wouldn't be triggered?

I have a WinForm which subscribes to the FormClosing event. When I click on the X, the event is triggered. However, when executing Close() on the WinForm, the event is not triggered. This WinForm isn't shown ever. Could that be the reason?

The event handlers are subscribed in the constructor of the WinForm (not in the Onload) so they are registered.

Upvotes: 1

Views: 74

Answers (1)

Hans Passant
Hans Passant

Reputation: 941585

This is by design, FormClosing will only fire if you actually made the form visible. Or to be more technically correct, when the native Windows window was created. If you never called Show() then Close() doesn't do anything at all. You only created the class object, FormClosing is not a substitute for a destructor.

Upvotes: 4

Related Questions