Detecting When An MDI Child Is Closing

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

I've found that if the user clicks the Close button or clicks to use the
Control box when closing a child form, it doesn't initiate an event in the
main form even though I've tied Ctrl+F4 to a main form event,
"FileClose_Click", for some additional processing. So, using Ctrl+F4 or
File\Close from the main menu work fine, but I need to tie in the mouse
clicks.

I've tried firing a user event when the "<form name>_Closed" and "<form
name>_Closing" events are fired to the listening main form so that
"FileClose_Click" can then be called. However, "FileClose_Click" expects
the child to be closed by this time and, evidently, it's not (I use
"this.ActiveMdiChild" to detect this).

Is there a way to do this? Thanks.


-Mike
 
Thanks for the reply, Herfried.

As I mentioned in the original post, I've already tried using a delegate to
notify the main form when the child is closed.

Is there another way?


-Mike
 
Hi Mike,
You won't get the Ctrl+F4 easily by just tieing it on a menu, because
it's an system-defined hotkey.
And I have some questions on your problem, From your description, I knew
you have an event handler
names FileClose_Click, is it implemented by yourself? and I didn't
understand the words
However, "FileClose_Click" expects
the child to be closed by this time and, evidently, it's not (I use
"this.ActiveMdiChild" to detect this).

If you can make you problem clearer , I think we can provide more solutions
or work arounds, thanks!




Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| From: "Mike" <[email protected]>
| Subject: Detecting When An MDI Child Is Closing
| Date: Thu, 4 Sep 2003 18:00:22 -0400
| Lines: 19
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <##C#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: dialup-171.75.39.10.dial1.washington1.level3.net
171.75.39.10
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:51667
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| I've found that if the user clicks the Close button or clicks to use the
| Control box when closing a child form, it doesn't initiate an event in the
| main form even though I've tied Ctrl+F4 to a main form event,
| "FileClose_Click", for some additional processing. So, using Ctrl+F4 or
| File\Close from the main menu work fine, but I need to tie in the mouse
| clicks.
|
| I've tried firing a user event when the "<form name>_Closed" and "<form
| name>_Closing" events are fired to the listening main form so that
| "FileClose_Click" can then be called. However, "FileClose_Click" expects
| the child to be closed by this time and, evidently, it's not (I use
| "this.ActiveMdiChild" to detect this).
|
| Is there a way to do this? Thanks.
|
|
| -Mike
|
|
|
 
Thanks for your reply, Ying-Shen.

Yes, I'm implementing FileClose_Click() myself in the parent form
when the user selects File\Close from the main menu
(see inline comments):


private void FileClose_Click(object sender, System.EventArgs e)
{
Form activeForm = this.ActiveMdiChild;

if( activeForm != null)
activeForm.Close();

// With only one child open, I expect activeForm to now be null.
activeForm = this.ActiveMdiChild;

// But, the form at this point is NOT null and the following code
// is NOT executed when the user uses the mouse to close the form.
if( activeForm == null )
{
mniView.Enabled = false;
mniChannel.Enabled = false;
}
}


FileClose_Click() works fine from the main menu (File\Close), and when
Ctrl+F4 is pressed.

However, when the user closes the child form with a mouse click (either
the control box Close or the "X" button), I call the above method through
a delegate. The inline comments describe what's happening in
this case.

How can I make this work? Thank you.


-Mike

Ying-Shen Yu said:
Hi Mike,
You won't get the Ctrl+F4 easily by just tieing it on a menu, because
it's an system-defined hotkey.
And I have some questions on your problem, From your description, I knew
you have an event handler
names FileClose_Click, is it implemented by yourself? and I didn't
understand the words
However, "FileClose_Click" expects
the child to be closed by this time and, evidently, it's not (I use
"this.ActiveMdiChild" to detect this).

If you can make you problem clearer , I think we can provide more solutions
or work arounds, thanks!




Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| From: "Mike" <[email protected]>
| Subject: Detecting When An MDI Child Is Closing
| Date: Thu, 4 Sep 2003 18:00:22 -0400
| Lines: 19
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <##C#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: dialup-171.75.39.10.dial1.washington1.level3.net
171.75.39.10
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:51667
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| I've found that if the user clicks the Close button or clicks to use the
| Control box when closing a child form, it doesn't initiate an event in the
| main form even though I've tied Ctrl+F4 to a main form event,
| "FileClose_Click", for some additional processing. So, using Ctrl+F4 or
| File\Close from the main menu work fine, but I need to tie in the mouse
| clicks.
|
| I've tried firing a user event when the "<form name>_Closed" and "<form
| name>_Closing" events are fired to the listening main form so that
| "FileClose_Click" can then be called. However, "FileClose_Click" expects
| the child to be closed by this time and, evidently, it's not (I use
| "this.ActiveMdiChild" to detect this).
|
| Is there a way to do this? Thanks.
|
|
| -Mike
|
|
|
 
Hi mike,
let me repeat your problem, to my understanding you want to get
informed when the last MdiChild window is closed?right? You may try this
way.
<code>
//put the code in your mainform
private void mnuNew_Click(object sender, System.EventArgs e)
{
ChildForm child = new ChildForm();
child.MdiParent = this;
child.Closing += new CancelEventHandler(child_Closing);
child.Closed += new EventHandler(child_Closed);
child.Show();
}

private void menuItem3_Click(object sender, System.EventArgs e)
{
Form activeForm = this.ActiveMdiChild;
if( activeForm != null)
activeForm.Close();
}

private void child_Closing(object sender, CancelEventArgs e)
{
//put your confirmation dialog here.
}

private void child_Closed(object sender, EventArgs e)
{
//the current form is closed and being removed from the array, but for
now
//it's still there.
if (this.MdiChildren.Length == 1)
{
MessageBox.Show("OnNoMdiChild()");
//OnNoMdiChild();
}
}
</code>
If you still have problems on this issue, please let me know,
thanks!

Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| From: "Mike" <[email protected]>
| References: <##C#[email protected]>
<[email protected]>
| Subject: Re: Detecting When An MDI Child Is Closing
| Date: Mon, 8 Sep 2003 12:05:24 -0400
| Lines: 112
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: dialup-171.75.39.63.dial1.washington1.level3.net
171.75.39.63
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:51882
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Thanks for your reply, Ying-Shen.
|
| Yes, I'm implementing FileClose_Click() myself in the parent form
| when the user selects File\Close from the main menu
| (see inline comments):
|
|
| private void FileClose_Click(object sender, System.EventArgs e)
| {
| Form activeForm = this.ActiveMdiChild;
|
| if( activeForm != null)
| activeForm.Close();
|
| // With only one child open, I expect activeForm to now be null.
| activeForm = this.ActiveMdiChild;
|
| // But, the form at this point is NOT null and the following code
| // is NOT executed when the user uses the mouse to close the form.
| if( activeForm == null )
| {
| mniView.Enabled = false;
| mniChannel.Enabled = false;
| }
| }
|
|
| FileClose_Click() works fine from the main menu (File\Close), and when
| Ctrl+F4 is pressed.
|
| However, when the user closes the child form with a mouse click (either
| the control box Close or the "X" button), I call the above method through
| a delegate. The inline comments describe what's happening in
| this case.
|
| How can I make this work? Thank you.
|
|
| -Mike
|
| | > Hi Mike,
| > You won't get the Ctrl+F4 easily by just tieing it on a menu,
because
| > it's an system-defined hotkey.
| > And I have some questions on your problem, From your description, I knew
| > you have an event handler
| > names FileClose_Click, is it implemented by yourself? and I didn't
| > understand the words
| > >However, "FileClose_Click" expects
| > >the child to be closed by this time and, evidently, it's not (I use
| > >"this.ActiveMdiChild" to detect this).
| >
| > If you can make you problem clearer , I think we can provide more
| solutions
| > or work arounds, thanks!
| >
| >
| >
| >
| > Kind regards,
| >
| > Ying-Shen Yu [MS]
| > Microsoft Support Engineer
| >
| > This posting is provided "AS IS" with no warranties, and confers no
| rights.
| > You assume all risk for your use. 2001 Microsoft Corporation. All
rights
| > reserved.
| > --------------------
| > | From: "Mike" <[email protected]>
| > | Subject: Detecting When An MDI Child Is Closing
| > | Date: Thu, 4 Sep 2003 18:00:22 -0400
| > | Lines: 19
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <##C#[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.windowsforms
| > | NNTP-Posting-Host: dialup-171.75.39.10.dial1.washington1.level3.net
| > 171.75.39.10
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| > microsoft.public.dotnet.framework.windowsforms:51667
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
| > |
| > | I've found that if the user clicks the Close button or clicks to use
the
| > | Control box when closing a child form, it doesn't initiate an event in
| the
| > | main form even though I've tied Ctrl+F4 to a main form event,
| > | "FileClose_Click", for some additional processing. So, using Ctrl+F4
or
| > | File\Close from the main menu work fine, but I need to tie in the
mouse
| > | clicks.
| > |
| > | I've tried firing a user event when the "<form name>_Closed" and
"<form
| > | name>_Closing" events are fired to the listening main form so that
| > | "FileClose_Click" can then be called. However, "FileClose_Click"
| expects
| > | the child to be closed by this time and, evidently, it's not (I use
| > | "this.ActiveMdiChild" to detect this).
| > |
| > | Is there a way to do this? Thanks.
| > |
| > |
| > | -Mike
| > |
| > |
| > |
| >
|
|
|
 
That worked!

Where should I detach the event handler,
i.e. "child.Closed -= new EventHandler(child_Closed)" ?

Thank you, Ying-Shen.


-Mike

Ying-Shen Yu said:
Hi mike,
let me repeat your problem, to my understanding you want to get
informed when the last MdiChild window is closed?right? You may try this
way.
<code>
//put the code in your mainform
private void mnuNew_Click(object sender, System.EventArgs e)
{
ChildForm child = new ChildForm();
child.MdiParent = this;
child.Closing += new CancelEventHandler(child_Closing);
child.Closed += new EventHandler(child_Closed);
child.Show();
}

private void menuItem3_Click(object sender, System.EventArgs e)
{
Form activeForm = this.ActiveMdiChild;
if( activeForm != null)
activeForm.Close();
}

private void child_Closing(object sender, CancelEventArgs e)
{
//put your confirmation dialog here.
}

private void child_Closed(object sender, EventArgs e)
{
//the current form is closed and being removed from the array, but for
now
//it's still there.
if (this.MdiChildren.Length == 1)
{
MessageBox.Show("OnNoMdiChild()");
//OnNoMdiChild();
}
}
</code>
If you still have problems on this issue, please let me know,
thanks!

Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| From: "Mike" <[email protected]>
| References: <##C#[email protected]>
<[email protected]>
| Subject: Re: Detecting When An MDI Child Is Closing
| Date: Mon, 8 Sep 2003 12:05:24 -0400
| Lines: 112
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: dialup-171.75.39.63.dial1.washington1.level3.net
171.75.39.63
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:51882
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Thanks for your reply, Ying-Shen.
|
| Yes, I'm implementing FileClose_Click() myself in the parent form
| when the user selects File\Close from the main menu
| (see inline comments):
|
|
| private void FileClose_Click(object sender, System.EventArgs e)
| {
| Form activeForm = this.ActiveMdiChild;
|
| if( activeForm != null)
| activeForm.Close();
|
| // With only one child open, I expect activeForm to now be null.
| activeForm = this.ActiveMdiChild;
|
| // But, the form at this point is NOT null and the following code
| // is NOT executed when the user uses the mouse to close the form.
| if( activeForm == null )
| {
| mniView.Enabled = false;
| mniChannel.Enabled = false;
| }
| }
|
|
| FileClose_Click() works fine from the main menu (File\Close), and when
| Ctrl+F4 is pressed.
|
| However, when the user closes the child form with a mouse click (either
| the control box Close or the "X" button), I call the above method through
| a delegate. The inline comments describe what's happening in
| this case.
|
| How can I make this work? Thank you.
|
|
| -Mike
|
| | > Hi Mike,
| > You won't get the Ctrl+F4 easily by just tieing it on a menu,
because
| > it's an system-defined hotkey.
| > And I have some questions on your problem, From your description, I knew
| > you have an event handler
| > names FileClose_Click, is it implemented by yourself? and I didn't
| > understand the words
| > >However, "FileClose_Click" expects
| > >the child to be closed by this time and, evidently, it's not (I use
| > >"this.ActiveMdiChild" to detect this).
| >
| > If you can make you problem clearer , I think we can provide more
| solutions
| > or work arounds, thanks!
| >
| >
| >
| >
| > Kind regards,
| >
| > Ying-Shen Yu [MS]
| > Microsoft Support Engineer
| >
| > This posting is provided "AS IS" with no warranties, and confers no
| rights.
| > You assume all risk for your use. 2001 Microsoft Corporation. All
rights
| > reserved.
| > --------------------
| > | From: "Mike" <[email protected]>
| > | Subject: Detecting When An MDI Child Is Closing
| > | Date: Thu, 4 Sep 2003 18:00:22 -0400
| > | Lines: 19
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <##C#[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.windowsforms
| > | NNTP-Posting-Host: dialup-171.75.39.10.dial1.washington1.level3.net
| > 171.75.39.10
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| > microsoft.public.dotnet.framework.windowsforms:51667
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
| > |
| > | I've found that if the user clicks the Close button or clicks to use
the
| > | Control box when closing a child form, it doesn't initiate an event in
| the
| > | main form even though I've tied Ctrl+F4 to a main form event,
| > | "FileClose_Click", for some additional processing. So, using Ctrl+F4
or
| > | File\Close from the main menu work fine, but I need to tie in the
mouse
| > | clicks.
| > |
| > | I've tried firing a user event when the "<form name>_Closed" and
"<form
| > | name>_Closing" events are fired to the listening main form so that
| > | "FileClose_Click" can then be called. However, "FileClose_Click"
| expects
| > | the child to be closed by this time and, evidently, it's not (I use
| > | "this.ActiveMdiChild" to detect this).
| > |
| > | Is there a way to do this? Thanks.
| > |
| > |
| > | -Mike
| > |
| > |
| > |
| >
|
|
|
 
Hi Mike,
I'm glad to hear that you solved the problem. And about your last
question,
I'm not clear about why you want to remove the handler manually, The
Handler will be removed when
the child form is GCed, although the form object will not be GCed
immediately, but since it has been closed,
the event will not be fired twice, right? I think you may safely leave this
to Winform and CLR.
If you do want to remove the handler explicitly, you may try removing it in
OnClose event.

If this is not helpful to your problem, please let me know.

Thanks!


Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| From: "Mike" <[email protected]>
| References: <##C#[email protected]>
<[email protected]>
<[email protected]>
<B#[email protected]>
| Subject: Re: Detecting When An MDI Child Is Closing
| Date: Tue, 9 Sep 2003 15:30:34 -0400
| Lines: 211
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: dialup-171.75.39.29.dial1.washington1.level3.net
171.75.39.29
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:51969
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| That worked!
|
| Where should I detach the event handler,
| i.e. "child.Closed -= new EventHandler(child_Closed)" ?
|
| Thank you, Ying-Shen.
|
|
| -Mike
|
| | > Hi mike,
| > let me repeat your problem, to my understanding you want to get
| > informed when the last MdiChild window is closed?right? You may try this
| > way.
| > <code>
| > //put the code in your mainform
| > private void mnuNew_Click(object sender, System.EventArgs e)
| > {
| > ChildForm child = new ChildForm();
| > child.MdiParent = this;
| > child.Closing += new CancelEventHandler(child_Closing);
| > child.Closed += new EventHandler(child_Closed);
| > child.Show();
| > }
| >
| > private void menuItem3_Click(object sender, System.EventArgs e)
| > {
| > Form activeForm = this.ActiveMdiChild;
| > if( activeForm != null)
| > activeForm.Close();
| > }
| >
| > private void child_Closing(object sender, CancelEventArgs e)
| > {
| > //put your confirmation dialog here.
| > }
| >
| > private void child_Closed(object sender, EventArgs e)
| > {
| > //the current form is closed and being removed from the array, but for
| > now
| > //it's still there.
| > if (this.MdiChildren.Length == 1)
| > {
| > MessageBox.Show("OnNoMdiChild()");
| > //OnNoMdiChild();
| > }
| > }
| > </code>
| > If you still have problems on this issue, please let me know,
| > thanks!
| >
| > Kind regards,
| >
| > Ying-Shen Yu [MS]
| > Microsoft Support Engineer
| >
| > This posting is provided "AS IS" with no warranties, and confers no
| rights.
| > You assume all risk for your use. 2001 Microsoft Corporation. All
rights
| > reserved.
| > --------------------
| > | From: "Mike" <[email protected]>
| > | References: <##C#[email protected]>
| > <[email protected]>
| > | Subject: Re: Detecting When An MDI Child Is Closing
| > | Date: Mon, 8 Sep 2003 12:05:24 -0400
| > | Lines: 112
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.windowsforms
| > | NNTP-Posting-Host: dialup-171.75.39.63.dial1.washington1.level3.net
| > 171.75.39.63
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| > microsoft.public.dotnet.framework.windowsforms:51882
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
| > |
| > | Thanks for your reply, Ying-Shen.
| > |
| > | Yes, I'm implementing FileClose_Click() myself in the parent form
| > | when the user selects File\Close from the main menu
| > | (see inline comments):
| > |
| > |
| > | private void FileClose_Click(object sender, System.EventArgs e)
| > | {
| > | Form activeForm = this.ActiveMdiChild;
| > |
| > | if( activeForm != null)
| > | activeForm.Close();
| > |
| > | // With only one child open, I expect activeForm to now be null.
| > | activeForm = this.ActiveMdiChild;
| > |
| > | // But, the form at this point is NOT null and the following code
| > | // is NOT executed when the user uses the mouse to close the form.
| > | if( activeForm == null )
| > | {
| > | mniView.Enabled = false;
| > | mniChannel.Enabled = false;
| > | }
| > | }
| > |
| > |
| > | FileClose_Click() works fine from the main menu (File\Close), and when
| > | Ctrl+F4 is pressed.
| > |
| > | However, when the user closes the child form with a mouse click
(either
| > | the control box Close or the "X" button), I call the above method
| through
| > | a delegate. The inline comments describe what's happening in
| > | this case.
| > |
| > | How can I make this work? Thank you.
| > |
| > |
| > | -Mike
| > |
| > | | > | > Hi Mike,
| > | > You won't get the Ctrl+F4 easily by just tieing it on a menu,
| > because
| > | > it's an system-defined hotkey.
| > | > And I have some questions on your problem, From your description, I
| knew
| > | > you have an event handler
| > | > names FileClose_Click, is it implemented by yourself? and I didn't
| > | > understand the words
| > | > >However, "FileClose_Click" expects
| > | > >the child to be closed by this time and, evidently, it's not (I use
| > | > >"this.ActiveMdiChild" to detect this).
| > | >
| > | > If you can make you problem clearer , I think we can provide more
| > | solutions
| > | > or work arounds, thanks!
| > | >
| > | >
| > | >
| > | >
| > | > Kind regards,
| > | >
| > | > Ying-Shen Yu [MS]
| > | > Microsoft Support Engineer
| > | >
| > | > This posting is provided "AS IS" with no warranties, and confers no
| > | rights.
| > | > You assume all risk for your use. 2001 Microsoft Corporation. All
| > rights
| > | > reserved.
| > | > --------------------
| > | > | From: "Mike" <[email protected]>
| > | > | Subject: Detecting When An MDI Child Is Closing
| > | > | Date: Thu, 4 Sep 2003 18:00:22 -0400
| > | > | Lines: 19
| > | > | X-Priority: 3
| > | > | X-MSMail-Priority: Normal
| > | > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | > | Message-ID: <##C#[email protected]>
| > | > | Newsgroups: microsoft.public.dotnet.framework.windowsforms
| > | > | NNTP-Posting-Host:
dialup-171.75.39.10.dial1.washington1.level3.net
| > | > 171.75.39.10
| > | > | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| > | > | Xref: cpmsftngxa06.phx.gbl
| > | > microsoft.public.dotnet.framework.windowsforms:51667
| > | > | X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
| > | > |
| > | > | I've found that if the user clicks the Close button or clicks to
use
| > the
| > | > | Control box when closing a child form, it doesn't initiate an
event
| in
| > | the
| > | > | main form even though I've tied Ctrl+F4 to a main form event,
| > | > | "FileClose_Click", for some additional processing. So, using
| Ctrl+F4
| > or
| > | > | File\Close from the main menu work fine, but I need to tie in the
| > mouse
| > | > | clicks.
| > | > |
| > | > | I've tried firing a user event when the "<form name>_Closed" and
| > "<form
| > | > | name>_Closing" events are fired to the listening main form so that
| > | > | "FileClose_Click" can then be called. However, "FileClose_Click"
| > | expects
| > | > | the child to be closed by this time and, evidently, it's not (I
use
| > | > | "this.ActiveMdiChild" to detect this).
| > | > |
| > | > | Is there a way to do this? Thanks.
| > | > |
| > | > |
| > | > | -Mike
| > | > |
| > | > |
| > | > |
| > | >
| > |
| > |
| > |
| >
|
|
|
|
|
 
Ying-Shen,

I had thought that it was always good practice to detach any event handlers
before exiting. If that's not necessary, then I won't worry about it.
Thank you for your help.


-Mike

Ying-Shen Yu said:
Hi Mike,
I'm glad to hear that you solved the problem. And about your last
question,
I'm not clear about why you want to remove the handler manually, The
Handler will be removed when
the child form is GCed, although the form object will not be GCed
immediately, but since it has been closed,
the event will not be fired twice, right? I think you may safely leave this
to Winform and CLR.
If you do want to remove the handler explicitly, you may try removing it in
OnClose event.

If this is not helpful to your problem, please let me know.

Thanks!


Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| From: "Mike" <[email protected]>
| References: <##C#[email protected]>
<[email protected]>
<[email protected]>
<B#[email protected]>
| Subject: Re: Detecting When An MDI Child Is Closing
| Date: Tue, 9 Sep 2003 15:30:34 -0400
| Lines: 211
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: dialup-171.75.39.29.dial1.washington1.level3.net
171.75.39.29
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:51969
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| That worked!
|
| Where should I detach the event handler,
| i.e. "child.Closed -= new EventHandler(child_Closed)" ?
|
| Thank you, Ying-Shen.
|
|
| -Mike
|
| | > Hi mike,
| > let me repeat your problem, to my understanding you want to get
| > informed when the last MdiChild window is closed?right? You may try this
| > way.
| > <code>
| > //put the code in your mainform
| > private void mnuNew_Click(object sender, System.EventArgs e)
| > {
| > ChildForm child = new ChildForm();
| > child.MdiParent = this;
| > child.Closing += new CancelEventHandler(child_Closing);
| > child.Closed += new EventHandler(child_Closed);
| > child.Show();
| > }
| >
| > private void menuItem3_Click(object sender, System.EventArgs e)
| > {
| > Form activeForm = this.ActiveMdiChild;
| > if( activeForm != null)
| > activeForm.Close();
| > }
| >
| > private void child_Closing(object sender, CancelEventArgs e)
| > {
| > //put your confirmation dialog here.
| > }
| >
| > private void child_Closed(object sender, EventArgs e)
| > {
| > //the current form is closed and being removed from the array, but for
| > now
| > //it's still there.
| > if (this.MdiChildren.Length == 1)
| > {
| > MessageBox.Show("OnNoMdiChild()");
| > //OnNoMdiChild();
| > }
| > }
| > </code>
| > If you still have problems on this issue, please let me know,
| > thanks!
| >
| > Kind regards,
| >
| > Ying-Shen Yu [MS]
| > Microsoft Support Engineer
| >
| > This posting is provided "AS IS" with no warranties, and confers no
| rights.
| > You assume all risk for your use. 2001 Microsoft Corporation. All
rights
| > reserved.
| > --------------------
| > | From: "Mike" <[email protected]>
| > | References: <##C#[email protected]>
| > <[email protected]>
| > | Subject: Re: Detecting When An MDI Child Is Closing
| > | Date: Mon, 8 Sep 2003 12:05:24 -0400
| > | Lines: 112
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.windowsforms
| > | NNTP-Posting-Host: dialup-171.75.39.63.dial1.washington1.level3.net
| > 171.75.39.63
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| > microsoft.public.dotnet.framework.windowsforms:51882
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
| > |
| > | Thanks for your reply, Ying-Shen.
| > |
| > | Yes, I'm implementing FileClose_Click() myself in the parent form
| > | when the user selects File\Close from the main menu
| > | (see inline comments):
| > |
| > |
| > | private void FileClose_Click(object sender, System.EventArgs e)
| > | {
| > | Form activeForm = this.ActiveMdiChild;
| > |
| > | if( activeForm != null)
| > | activeForm.Close();
| > |
| > | // With only one child open, I expect activeForm to now be null.
| > | activeForm = this.ActiveMdiChild;
| > |
| > | // But, the form at this point is NOT null and the following code
| > | // is NOT executed when the user uses the mouse to close the form.
| > | if( activeForm == null )
| > | {
| > | mniView.Enabled = false;
| > | mniChannel.Enabled = false;
| > | }
| > | }
| > |
| > |
| > | FileClose_Click() works fine from the main menu (File\Close), and when
| > | Ctrl+F4 is pressed.
| > |
| > | However, when the user closes the child form with a mouse click
(either
| > | the control box Close or the "X" button), I call the above method
| through
| > | a delegate. The inline comments describe what's happening in
| > | this case.
| > |
| > | How can I make this work? Thank you.
| > |
| > |
| > | -Mike
| > |
| > | | > | > Hi Mike,
| > | > You won't get the Ctrl+F4 easily by just tieing it on a menu,
| > because
| > | > it's an system-defined hotkey.
| > | > And I have some questions on your problem, From your description, I
| knew
| > | > you have an event handler
| > | > names FileClose_Click, is it implemented by yourself? and I didn't
| > | > understand the words
| > | > >However, "FileClose_Click" expects
| > | > >the child to be closed by this time and, evidently, it's not (I use
| > | > >"this.ActiveMdiChild" to detect this).
| > | >
| > | > If you can make you problem clearer , I think we can provide more
| > | solutions
| > | > or work arounds, thanks!
| > | >
| > | >
| > | >
| > | >
| > | > Kind regards,
| > | >
| > | > Ying-Shen Yu [MS]
| > | > Microsoft Support Engineer
| > | >
| > | > This posting is provided "AS IS" with no warranties, and confers no
| > | rights.
| > | > You assume all risk for your use. 2001 Microsoft Corporation. All
| > rights
| > | > reserved.
| > | > --------------------
| > | > | From: "Mike" <[email protected]>
| > | > | Subject: Detecting When An MDI Child Is Closing
| > | > | Date: Thu, 4 Sep 2003 18:00:22 -0400
| > | > | Lines: 19
| > | > | X-Priority: 3
| > | > | X-MSMail-Priority: Normal
| > | > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | > | Message-ID: <##C#[email protected]>
| > | > | Newsgroups: microsoft.public.dotnet.framework.windowsforms
| > | > | NNTP-Posting-Host:
dialup-171.75.39.10.dial1.washington1.level3.net
| > | > 171.75.39.10
| > | > | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| > | > | Xref: cpmsftngxa06.phx.gbl
| > | > microsoft.public.dotnet.framework.windowsforms:51667
| > | > | X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
| > | > |
| > | > | I've found that if the user clicks the Close button or clicks to
use
| > the
| > | > | Control box when closing a child form, it doesn't initiate an
event
| in
| > | the
| > | > | main form even though I've tied Ctrl+F4 to a main form event,
| > | > | "FileClose_Click", for some additional processing. So, using
| Ctrl+F4
| > or
| > | > | File\Close from the main menu work fine, but I need to tie in the
| > mouse
| > | > | clicks.
| > | > |
| > | > | I've tried firing a user event when the "<form name>_Closed" and
| > "<form
| > | > | name>_Closing" events are fired to the listening main form so that
| > | > | "FileClose_Click" can then be called. However, "FileClose_Click"
| > | expects
| > | > | the child to be closed by this time and, evidently, it's not (I
use
| > | > | "this.ActiveMdiChild" to detect this).
| > | > |
| > | > | Is there a way to do this? Thanks.
| > | > |
| > | > |
| > | > | -Mike
| > | > |
| > | > |
| > | > |
| > | >
| > |
| > |
| > |
| >
|
|
|
|
|
 
Back
Top