e.Cancel = True in MDI Child Closing event

O

Oenone

I'm writing an application with various MDI child forms.

In the Closing event of many of the forms, I have code that asks the user
whether he is sure he wants to close the form, because it contains unsaved
data. If the user clicks "No" then the code sets e.Cancel = True, which
cancels the closure. This works just great.

But when the user closes the MDI parent form, the cancellation seems to be
completely ignored. The MessageBox appears, I click "No", but the form (and
the application) closes anyway.

In VB6, aborting the closing in a form's QueryUnload event would cancel the
closure of the application in this circumstance. I'm eager to try to obtain
this behaviour in VB.NET as I'm worried about people losing work that they
have entered into these forms.

Is there any way to achieve this?

Many thanks,
 
C

Charles Law

The expected behaviour is that if you attempt to close an MDI parent then
the Closing event of each MDI child will fire. If any one of these child
forms sets e.Cancel = True then no further Closing events will be fired, and
the MDI parent will not close. I have this scenario and it works as
described.

Can you confirm that with one MDI parent and one Child, clicking the MDI
Parent close button does not fire the child closing event?

Charles
 
O

Oenone

Charles said:
The expected behaviour is that if you attempt to close an MDI parent
then the Closing event of each MDI child will fire. If any one of
these child forms sets e.Cancel = True then no further Closing events
will be fired, and the MDI parent will not close. I have this
scenario and it works as described.

Hmm, I just set up a project to test this and it worked exactly as you
described.

There must be something else going on in my real application -- I'll
investigate.

Thanks for your help,
 
O

Oenone

Oenone said:
There must be something else going on in my real application -- I'll
investigate.

In the Closing event of the MDI Container form, I had some code which closed
the entire application down. This was a hang-over from the VB6
implementation and was unsurprisingly causing the behaviour I described.
With this removed it's all working perfectly.

Thanks again,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top