Know when an mdi child closes?

  • Thread starter Thread starter SStory
  • Start date Start date
S

SStory

How can the MDI parent form know when an MDI child closes?

I need to know when the last one closes because I have something on the MDI
form that I need to clear out when all children are closed...

Thanks,

Shane
 
Hi,

there are more ways to solve this.
you can use a global counter and increment or decrement it while opening and
closing forms.
but there is a build in counter in the MDIParent window. use these to get
the mdiclient forms count:

[ParentFormName].MdiChildren.Length

Use a callback from mdichilds or something like that to inform the parent
window.
this can be done with subclassing or eventhandlers for the windows. its up
to you...

check this for more information:

http://msdn.microsoft.com/library/d...stemWindowsFormsFormClassMdiChildrenTopic.asp

This is the best solution without any workaround...

Best regards

Kerem Gümrükcü
 
Yes... well I am need notification. There is no MdiChildClosed event
There is the MdiChildActivated.....
bummer...

Guess I will have to have the child call the parent someway to say closing
and then see if any children are open.

Shane
 
Shane,

I did not try this, however I would try it raising a public event in my
clossing event and catch that in my mainMDI form, when I had this to solve.

I hope this helps a little bit.

Cor
Yes... well I am need notification. There is no MdiChildClosed event
There is the MdiChildActivated.....
bummer...

Guess I will have to have the child call the parent someway to say closing
and then see if any children are open.

Shane
Kerem Gümrükcü said:
Hi,

there are more ways to solve this.
you can use a global counter and increment or decrement it while opening and
closing forms.
but there is a build in counter in the MDIParent window. use these to get
the mdiclient forms count:

[ParentFormName].MdiChildren.Length

Use a callback from mdichilds or something like that to inform the parent
window.
this can be done with subclassing or eventhandlers for the windows. its up
to you...

check this for more information:
http://msdn.microsoft.com/library/d...stemWindowsFormsFormClassMdiChildrenTopic.asp
This is the best solution without any workaround...

Best regards

Kerem Gümrükcü
 
Shane,
I would use the techniques mentioned in the Thread that Herfried showed.

The sample Herfried mentions assuming that there will only be one instance
of the child, if you can have multiple instances of the child forms, instead
of m_child As Form2 you may want to keep a collection of children...

As Jens pointed out, I think the MdiChildActivated event will also tell you
what you want.

Hope this helps
Jay
 
Hey man--this is good news!! Thanks a bunch I will try it out.
Most importantly I am needing to know when the last one has closed to clean
up shared resources.

Thanks... I will check it out.

Shane
 
Yeah... it thought about the forms collection but doesn't mdi parent already
have a collection? So seems like duplication for nothing and my real issue
was being informed of when one closed so I could decide if it were the last
one.

I think Jens's suggestion may be just what I need and I can check
mdichildren count or whatever the mdichild collection is named--I'm not in
the IDE right now...

Thanks.
 
hmm..

Adding a handler to the MDI child that points back to the MDI parent sounds
like a good potential.

Now when I do this....if also in the MDIchild's close event I put some code,
would both execute and which would be first?

Thanks,

Shane
 
* "SStory said:
Adding a handler to the MDI child that points back to the MDI parent sounds
like a good potential.

Now when I do this....if also in the MDIchild's close event I put some code,
would both execute and which would be first?

This depends on which order the event handlers are added. Typically the
handler defined in the main form will execute after the event handler
defined in the child form.
 

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

Back
Top