Catching MDI Application Closing

R

Ron L

I have an MDI application with a number of child windows. In each child
window I am catching the Closing event and having the child window decide if
it should set cancel to true. The intent here is to ensure that no child
window can close while it is in a state where user entered information can
be lost. I have just noticed that while the Closing event is caught if I
click the X on the child window, it is not caught if I click the X on the
MDI parent window. Is there some other event that I am missing here?

Thanks,
Ron L
 
I

Ivar

Hi,

What about:

mainMID.Closing += new
System.ComponentModel.CancelEventHandler(this.Window_Closing);

private void Window_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
// Loop all childs and see if can close or save or ... do what you need
foreach(Form frm in this.MdiChildren){
}
}

At least this works ok for me.
 
R

Ron L

Ivar

Thanks for the response, I'll give it a try. Do you know why the Closing
event on the child windows only get thrown when the child window is closed
directly? I had thought that they would be thrown for any cause of the
window closing.

Ron L
 
R

Ron L

Ivar

I am closing the application, and all of its child windows, by clicking on
the "X" or Form.Close on the MDI Parent form. I had thought that the
application closing would fire a Closing event to each of the child windows,
but this is apparently not happening.

Thanks,
Ron L
 
R

Ron L

Ivar

I tried the loop you suggested, but I still don't seem to be able to catch
the cancel from the individual windows closing to be able to keep the
application from exiting. Do you have any suggestions on this?

Here is my close handler:
' 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
MessageBox.Show("Exiting the app!")
For Each frm As Form In _mainView.MdiChildren
frm.Close()
Next
End Sub

Thanks,
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