PLS HELP!!! MDI application Closing event

M

MuZZy

Hi,

For example, I have an MDI application with two MDI child forms: one
called "Menu" and one called "Data". "Menu" form has Menu_Closing()
event handler for "Closing" event where it checks if "Data" form exists
and if yes, it closes it.

Something like this:
private void Menu_Closing(object sender, CancelEventArgs e)
{
if (DataForm != null)
DataForm.Close();
}

Now, i need to add the following thing to the application: when you
close the MDI parent screen it shoudl ask you to confirm closing. smth
like "Do you reall want to close the app?", so i do it in MDIParent's
closing event handler. But here's the problem: when i click "X" to close
MDI Parent screen it first tries to close all MDI child screens even
before firing MDIParent_Closing, so Menu_Closing() gets fired and "Data"
form gets closed and only after that i get the message asking if i want
to close the application, which is of course unacceptable.

Any ideas how to overcome the problem?

Thank you in advance!
MuZZy
 
M

MuZZy

Hi Bryan,

Thank you for response! But i'm not sure what you mean... Do you mean to
wire MDIParent_Closing to MDIChild.Closing event? But i only need
MDIParent_Closing fired if we actually are closing the aplication, but
if i wire it to child form's Closing, it will be fired every time i just
want to close "Menu" form (MDI Child).
 
L

Linda Liu [MSFT]

Hi MuZZy,

I performed a test based on your description and saw the same thing as you
did. When I click the close button on the MDI parent form's title bar, the
"Data" form is closed first and then a message box appears asking if I want
to close the application.

As you have mentioned, the reason is that the MDI child form's Closing
event is fired before the MDI child form's Closing event is fired.

In fact, when we click the close button on an MDI parent or call the Close
method of the MDI parent, the Closing and Closed events of an MDI child and
its MDI parent occur in the following sequence:

MDI child's Closing

MDI parent's Closing

MDI child's Closed

MDI parent's Closed


So to solve the problem, you could handle the Closed event of the "Menu"
form, instead of the Closing event to close the "Data" form if it exits.

Hope this helps.

If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

MuZZy

Hi Linda,

Well, the way th eapp really works i can't close "Menu" child until
"Data" child is closed, so i can't do DataForm.Close() in
MenuForm.Closed event handler.

Any other ideas?

Thanks,
MuZZy
 
L

Linda Liu [MSFT]

Hi MuZZy,

Thank you for your prompt response.

When the Closed event of the "Menu" form is fired, the Menu form has not
been closed yet. Then we call the DataForm.Close method in the Menu form's
Closed event handler. The fact is that the "Data" form is closed before the
"Menu" form is closed. Do you agree with me?

As for other options, after doing some research, I found that the message
WM_CLOSE is sent to "Menu" form only when I click the Close button on its
title bar or call the Close method of the "Menu" form. So we could override
the WndProc function in the "Menu" form to trap the WM_CLOSE message.

The following is the sample code.

const int WM_CLOSE = 0x0010;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_CLOSE)
{
if (DataForm != null)
DataForm.Close();
}
base.WndProc (ref m);
}

For your information, if you use VS2005 in the future, you have a third
option to do it, i.e. handle the FormClosing event of the "Menu" form and
get the close reason from the FormClosingEventArgs's CloseReason property.
If the reason is user closing, close the "Data" form, otherwise, don't
close it.

The following is a sample.

void MenuForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (DataForm != null && e.CloseReason ==
CloseReason.UserClosing)
{
DataForm.Close();
}
}

Hope this helps.
If you have any concerns, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 

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