Closing Event of MDI Parent

  • Thread starter Thread starter Matthias Hess
  • Start date Start date
M

Matthias Hess

I am working on an MDI App. I want the Application to behave like Visual
Studio when the user closes the MDI Parent form.

Let me give you two examples how the application should work:

Example A
1. The user has opened the application, which displays a MDIParent ("MAIN")
form which contains two MDIChild Forms ("CHILD_A" and "CHILD_B").
2. Now the user makes some changes in CHILD_A and CHILD_B.
3. The User clicks the red cross in the upper right corner of MAIN.
4. The Application should display ONLY ONE Dialog box, which says that there
are unsaved changes in two windows.

Example B:
1. The user has opened the application, which displays a MDIParent ("MAIN")
form which contains two MDIChild Forms ("CHILD_A" and "CHILD_B").
2. Now the user makes some changes in CHILD_A and CHILD_B.
3. The User clicks the red cross in the upper right corner of CHILD_A.
4. The Application should display a Dialog box, which says that there are
unsaved changes in CHILD_A.

To be able to solve the Problem stated in Example B, I need to handle the
closing event in the child form, no problem. But this Dialog Box also
appears when I run the "Example A". So, in this case I see two dialog boxes
appearing, one from CHILD_A and one from CHILD_B, which is not what I
intend...

I cannot catch the closing event of the parent form BEFORE the child forms
receive their Closing Events. Thus, my child forms have no way of knowing
whether they are in an "Example A" or in an "Example B" case.

Any ideas?

Regards Mat
 
Wow. You're right, it would be nice if the Framework had support for this.

You could probably do your "save any of these files?" prompt in response to
WM_SYSCOMMAND (when the user actually clicks the close button, and before
Windows kicks off the close-query and close stuff in response to that
click). Try something like this (untested):

protected override void WndProc(ref System.Windows.Forms.Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xf060;

bool passMessage = true;

if (m.Msg == WM_SYSCOMMAND)
if ((m.WParam.ToInt32()) & 0xFFF0 == SC_CLOSE)
passMessage = PromptToSaveAll(); // return false if canceled

if (passMessage)
base.WndProc(ref m);
}

Obviously, this only looks for close-button clicks -- you would also need to
call your PromptToSaveAll() method when your user clicks the File > Exit
menu item, and anywhere else that you exit the app.
 
Joe

This is exactly what I was looking for, it works perfectly. thanks!

Regards
Matthias Hess
 
or cleaner to set the cancel event args = true.

No need to wrap native win32 then. (which MS should have provided).
 
Back
Top