Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / November 2006

Tip: Looking for answers? Try searching our database.

PLS HELP!!! MDI application Closing event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MuZZy - 07 Nov 2006 17:16 GMT
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
Bryan Phillips - 07 Nov 2006 17:45 GMT
When the child forms are created, add an event handler for the child
form's closing event using the method you created in the MDI parent
form.

Bryan Phillips
MCSD, MCDBA, MCSE
Blog:  http://bphillips76.spaces.live.com

> Hi,
>
[quoted text clipped - 23 lines]
> Thank you in advance!
> MuZZy
MuZZy - 07 Nov 2006 18:21 GMT
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).

> When the child forms are created, add an event handler for the child
> form's closing event using the method you created in the MDI parent form.
[quoted text clipped - 30 lines]
>> Thank you in advance!
>> MuZZy
Linda Liu [MSFT] - 08 Nov 2006 04:03 GMT
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.
MuZZy - 08 Nov 2006 06:14 GMT
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

> Hi MuZZy,
>
[quoted text clipped - 49 lines]
>  
> This posting is provided "AS IS" with no warranties, and confers no rights.
Linda Liu [MSFT] - 08 Nov 2006 09:23 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.