How to cancel MDI Application Closing from child windows

R

Ron L

I have an MDI application which opens a number of child windows, each of
which could have data in a state that needs to be saved. Each child window
catches its Closing event and cancels it if the user wants to save the data,
but when the Application closes this event isn't thrown automatically. I
added a loop to close each child window, but can't seem find how to catch
the child window's cancel of the Closing event, so even if the user selects
cancel to the closing event the application closes anyway. Can anyone point
me in the right direction here?

Here is my application closing handler which closes each child window:
' Catch the application exit event and gracefully close all MDI child
windows
Private Sub ApplicationExiting(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.CancelEventArgs)

Handles _mainView.Closing
For Each frm As Form In _mainView.MdiChildren
frm.Close()
Next
End Sub

TIA
Ron L

P.S. sorry for the cross-post, I forgot to add these groups before I clicked
send. DOH!
 
A

AMDRIT

There are a couple of things you can do, and it is up to you how you
implement them.

My recommendations

First: Do not issue the Application.Exit or Application.End until after all
your child forms are closed and all state data is preserved.

Second: On _mainView_Closing count the number of opened mdichildren and
set e.Cancel = true if the number is greater than 0

Third: Move your Application.End or Application.Exit statement in either
_mainView.Closed or more remote location.


sample:

sub _mainView_Closing

for each f as form in _mainView.mdichildren
f.close
next

if _mainView.mdichildren.length > 0 then e.cancel = true

end sub

sub _mainView_Closed
End
end sub
 
R

Ron L

AMDRIT

Thank you for your response. It worked great! I had been trying to pass on
the cancel rather than just see if anything canceled after the fact.

Ron L
 

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