Put If e.Cancel Then Exit Sub in Closing event

A

Academic

Does it make sense to put this

If e.Cancel Then Exit Sub

at the beginning of form closing events so if the user cancels the app's
exiting in one Closing routine he will not be asked again by another when
its form Closing routine is run?

I guess what I'm asking is will that work. If one form sets e.cancelled to
true will e.cancel be true when the next form receives a closing event?

Does your answer also apply the MdiChildren?



Thanks
 
M

Michael C

Academic said:
Does it make sense to put this

If e.Cancel Then Exit Sub

at the beginning of form closing events so if the user cancels the app's
exiting in one Closing routine he will not be asked again by another when
its form Closing routine is run?

I guess what I'm asking is will that work. If one form sets e.cancelled to
true will e.cancel be true when the next form receives a closing event?

Does your answer also apply the MdiChildren?

If one form receives a closing event and cancels it, then no other forms
will receive the closing event. So your code is unnecessary.
 
C

Chris Dunaway

Michael said:
If one form receives a closing event and cancels it, then no other forms
will receive the closing event. So your code is unnecessary.

Just wondering, will the forms always receive the event in a
predictable order?
 
A

Academic

I hate to generalize from a test so I won't. But in my situation a test
shows that if I have a MdiContainer open and a bunch of standard forms also
open and a bunch of MdiChildren also open, the all the standard forms
receive the Closing event even if one cancels. I'm closing the forms one at
a time in the MdiContainer Closing routine so maybe that makes a difference.

Also, e.Cancel is always false on entering except for MdiChildren where
e.Cancel remains true once a closing event sets it to True.

Not very neat.

Also, in this test, the forms received the event in the order they were
open - made no difference which had focus or which was topmost
 
A

Academic

I found this on the Internet. Agrees with my test except for the
MdiChildren value of e.cancel. Also, I can't think of a situation where if a
user canceled the app shutdown you would want to run all the other form
events but I suppose it could happen.

guess it's time to move on.

Thanks again

The event is broadcast by the multi-cast delegate just like any other
event. This delegate has no knowledge of the Cancel property or what
it is for. It always sends the event to all subscribed delegates.


Also, there are situations where you have multiple listeners of the
closing event and you want them to all fire off even if one of them
says to cancel.


If what you're doing doesn't need to run if something else cancelled,
then it's good practice to add this check at top of routine.
 

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