Closing form event

  • Thread starter Thread starter Chris Bruce
  • Start date Start date
C

Chris Bruce

In my application I need a way to distiguish between the following events:

1. When a user closes an MDI child window.
2. When the user closes the MDI parent window which subsequently closes
the MDI child window.

My application does certain logic when the user actually closes the MDI
child form by clicking the "X" in the upper right hand. My application,
however, should not execute this logic if the user closes the MDI parent. I
tried handling the Form.Closing event on the child window. The problem is,
that in both of the cases above, the sender is the MDI child
window--therefore the event handler doesn't know who actually closed the
form. More bad news for me: the MDI child Form.Closing event occurs before
the Parent Form.Closing event (so I can't set a flag or something on the
parent closing event to let the child closing events know that they should
not execute the application logic.)

What trick do I need to know about?

Thanks!
 
Chris said:
In my application I need a way to distiguish between the following events:

1. When a user closes an MDI child window.
2. When the user closes the MDI parent window which subsequently closes
the MDI child window.

My application does certain logic when the user actually closes the MDI
child form by clicking the "X" in the upper right hand. My application,
however, should not execute this logic if the user closes the MDI parent. I
tried handling the Form.Closing event on the child window. The problem is,
that in both of the cases above, the sender is the MDI child
window--therefore the event handler doesn't know who actually closed the
form. More bad news for me: the MDI child Form.Closing event occurs before
the Parent Form.Closing event (so I can't set a flag or something on the
parent closing event to let the child closing events know that they should
not execute the application logic.)

What trick do I need to know about?

I see a way to set both handlers, Closing, and Close and utilize some
kind of global flag.

Child_Closing() {
_applicationQuit = false;
}
MainForm_Closing() {
_applicationQuit = true;
}
Child_Close() {
if (!_applicationQuit)
DoThis();
}

As soon as main form Closing event is called after any child Closing,
but before any child Close, you have a chance here to know either this
is application exit or child window close.

Probably it is also possible to achive by overriding main form's WndProc
and setting the flag if WM_QUIT has been passed.

Vadim Chekan.
 
Back
Top