By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,929 Members | 631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,929 IT Pros & Developers. It's quick & easy.

Detect when no mdi child windows displayed

P: n/a
I can check for MdiChildren.Length=0, but which event handler should I put
this in to detect when a child window is closed?

TIA
Phil.
Aug 1 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
"Phil" <N/A>'s wild thoughts were released on Wed, 1 Aug
2007 16:08:34 +0100 bearing the following fruit:
>I can check for MdiChildren.Length=0, but which event handler should I put
this in to detect when a child window is closed?
Another one I'm wondering why?
--
Jan Hyde

https://mvp.support.microsoft.com/profile/Jan.Hyde
Aug 1 '07 #2

P: n/a
On Aug 1, 11:08 am, "Phil" <N/Awrote:
I can check for MdiChildren.Length=0, but which event handler should I put
this in to detect when a child window is closed?

TIA
Phil.
The child forms should expose a FormClosed event that you could add an
event handler to when you add them to your mdi form.

I also am curious like Jan, what reason do you have for doing this?

Thanks,

Seth Rowe

Aug 1 '07 #3

P: n/a
>>I can check for MdiChildren.Length=0, but which event handler should I put
>>this in to detect when a child window is closed?

Another one I'm wondering why?
I'm not sure why you need to know why, but since you asked: In this
particular application, when one or more mdi child forms are displayed, the
application might be considered 'active'. If there are no forms displayed,
the application is basically inactive, so there are various things I might
want to happen in this situation. It might be as simple as doing some
drawing on the background, or there might be some other housekeeping tasks
that can be done at this stage.
I could put some code in the Close event for each child form, but I may have
several different forms, so it would be nicer if the mdi parent could detect
when there are no more child forms left. If there is an event that is fired
in the parent each time a child form is closed, I can check at that point
how many children are remaining. Is this possible?
Thanks
Phil.
Aug 1 '07 #4

P: n/a
The child forms should expose a FormClosed event that you could add an
event handler to when you add them to your mdi form.
I am OK with adding event handlers for controls on a form, but I haven't
done anything with adding event handlers for forms created at runtime. I've
started looking at the help page for AddHandler, and I'll see if I can
figure it out. Thanks for the pointer.

Cheers,
Phil.
Aug 1 '07 #5

P: n/a
I'm not sure why you need to know why, but since you asked:

Since I also asked, I'll provide my reasoning. First off, I was just
plain curious - not much is normally done when all the mdi children
are closed, so I was just wondering what you were doing. Secondly, and
probably more importantly, knowing the reason why you are doing
something allows us to suggest a possible better way of achieving the
desired results. For example, if you mentioned you were looking at
open children to determine idle time, we could suggest something like
using the GetLastInputInfo API function.

Thanks,

Seth Rowe

Aug 1 '07 #6

P: n/a

"Phil" <N/Awrote in message
news:46**********************@ptn-nntp-reader02.plus.net...
>I can check for MdiChildren.Length=0, but which event handler should I put
this in to detect when a child window is closed?
Found it.
The MDIChildActivate event is fired each time a child window is closed, so I
can put my check in there.
Aug 1 '07 #7

P: n/a
On Aug 1, 12:51 pm, "Phil" <N/Awrote:
"Phil" <N/Awrote in message

news:46**********************@ptn-nntp-reader02.plus.net...>I can check for MdiChildren.Length=0, but which event handler should I put
this in to detect when a child window is closed?

Found it.
The MDIChildActivate event is fired each time a child window is closed, so I
can put my check in there.
The MDIChildActivate will also fire at other times - not only when a
Child is closed. Why not just use AddHandler to listen to the child's
FormClosed event? This event was built to notify listeners of this
specific event.

Thanks,

Seth Rowe

Aug 1 '07 #8

P: n/a
On Aug 1, 11:08 am, "Phil" <N/Awrote:
I can check for MdiChildren.Length=0, but which event handler should I put
this in to detect when a child window is closed?
I am currently working on an MDI application that had a very similar
requirement. If a child window was opened, I set the visible property
to two toolbar icons on the Main Form's toolbar to true, these two
toolbar icons cause code to be called in the current active MDI child.
When the last child window was closed, I wanted to reset the visible
property to these two icons to false, so that they were only visible
if a child window was open.

I accomplished this task by:

1. Create a Integer property in the Main Form class that contained the
current number of active MDI children. Any event in the Main Form that
created a child window bumped this counter up by one.

2. Create a property in the Child Form Class of the Main Form
datatype. In each event in the Main Form that created a child form,
before showing the form, I set this property to Me (the parent form).

3. In the Child Form's FormClosing Event, I added code to decrement
the parent form's current child window count by one, and if the value
is now zero, set the visible property of the two toolbar icons on the
Main Form's toolbar to false.

There may have been a simpler way to do it, but it has the distinct
property of working.

In my case, only the child form needed to know if it was the last
child window being closed, but since the proeprty that defines the
current number of open child windows was in the Main Form, any code in
the Main Form had access to it as well.

Aug 1 '07 #9

P: n/a
rowe_newsgroups <ro********@yahoo.com>'s wild thoughts were
released on Wed, 01 Aug 2007 09:41:07 -0700 bearing the
following fruit:
>I'm not sure why you need to know why, but since you asked:

Since I also asked, I'll provide my reasoning. First off, I was just
plain curious - not much is normally done when all the mdi children
are closed, so I was just wondering what you were doing. Secondly, and
probably more importantly, knowing the reason why you are doing
something allows us to suggest a possible better way of achieving the
desired results. For example, if you mentioned you were looking at
open children to determine idle time, we could suggest something like
using the GetLastInputInfo API function.
My reasons were the same.

To the OP

I would also have added that in my experience users don't
close down forms when they leave their PC for example. So it
probably would not be a good way to determine idle time. As
already demonstrated by rowe there is a better solution to
your problem.

--
Jan Hyde

https://mvp.support.microsoft.com/profile/Jan.Hyde
Aug 2 '07 #10

P: n/a

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
On Aug 1, 12:51 pm, "Phil" <N/Awrote:
>"Phil" <N/Awrote in message

news:46**********************@ptn-nntp-reader02.plus.net...>I can check
for MdiChildren.Length=0, but which event handler should I put
>this in to detect when a child window is closed?

Found it.
The MDIChildActivate event is fired each time a child window is closed,
so I
can put my check in there.

The MDIChildActivate will also fire at other times - not only when a
Child is closed.
It doesn't matter that the event will fire at other times, as long as I know
that it will fire when a child form is closed, I can check how many forms
are displayed, and do what ever I need to when this is zero.
Why not just use AddHandler to listen to the child's
FormClosed event? This event was built to notify listeners of this
specific event.
I did start looking into this, as per your suggestion. The main problem is
that I will have to call AddHandler every time a new child form is created.
Some child forms may launch other child forms, so the code for loading the
forms won't necessarily all be in the parent form class (not even
necessarily in the same code project), and if I add any further forms in the
future I will always have to remember to add the handler. It is much simpler
if the parent can handle this itself.

Cheers,
Phil.
Aug 2 '07 #11

P: n/a
I am currently working on an MDI application that had a very similar
requirement. If a child window was opened, I set the visible property
to two toolbar icons on the Main Form's toolbar to true, these two
toolbar icons cause code to be called in the current active MDI child.
When the last child window was closed, I wanted to reset the visible
property to these two icons to false, so that they were only visible
if a child window was open.

I accomplished this task by:

1. Create a Integer property in the Main Form class that contained the
current number of active MDI children.
This property already exists (MdiChildren.Length) so you can simplify your
code by using this..
Any event in the Main Form that
created a child window bumped this counter up by one.
What happens if a child form is created in some other part of the code? You
would have to make the property public, or provide a public method to keep
track of it.
MdiChildren.Length is updated automatically.
>
2. Create a property in the Child Form Class of the Main Form
datatype. In each event in the Main Form that created a child form,
before showing the form, I set this property to Me (the parent form).
Aren't you just duplicating the MDIParent property?
>
3. In the Child Form's FormClosing Event, I added code to decrement
the parent form's current child window count by one, and if the value
is now zero, set the visible property of the two toolbar icons on the
Main Form's toolbar to false.
You would have to add this code to every child form. If you only have one
type of form that's OK I suppose. Better to have this code in the parent
really though.
>
There may have been a simpler way to do it, but it has the distinct
property of working.
That is always good :-)
I think the way I have found is much simpler though
All you needed to do was handle the MDIChildActivate event, check if
MdiChildren.Length=0, and update your toolbar as appropriate.
>
In my case, only the child form needed to know if it was the last
child window being closed, but since the proeprty that defines the
current number of open child windows was in the Main Form, any code in
the Main Form had access to it as well.
In my case none of my child forms, need to know whether they are the last
form or not. Much simpler, I think.
Aug 2 '07 #12

P: n/a
On Aug 2, 5:32 am, "Phil" <N/Awrote:
I am currently working on an MDI application that had a very similar
requirement. If a child window was opened, I set the visible property
to two toolbar icons on the Main Form's toolbar to true, these two
toolbar icons cause code to be called in the current active MDI child.
When the last child window was closed, I wanted to reset the visible
property to these two icons to false, so that they were only visible
if a child window was open.
I accomplished this task by:
1. Create a Integer property in the Main Form class that contained the
current number of active MDI children.

This property already exists (MdiChildren.Length) so you can simplify your
code by using this..
You know, I looked for a property the defined the number of current
child windows, but I saw Length instead of Count and interpeted that
to mean something else. I appreciate the comment, it did simplify my
code.
>
Any event in the Main Form that
created a child window bumped this counter up by one.

What happens if a child form is created in some other part of the code? You
would have to make the property public, or provide a public method to keep
track of it.
MdiChildren.Length is updated automatically.
2. Create a property in the Child Form Class of the Main Form
datatype. In each event in the Main Form that created a child form,
before showing the form, I set this property to Me (the parent form).

Aren't you just duplicating the MDIParent property?
This was another property I had misused. I have now figured out what I
was doing wrong and am now using this property as well. Thanks for
reminding me.
>

3. In the Child Form's FormClosing Event, I added code to decrement
the parent form's current child window count by one, and if the value
is now zero, set the visible property of the two toolbar icons on the
Main Form's toolbar to false.

You would have to add this code to every child form. If you only have one
type of form that's OK I suppose. Better to have this code in the parent
really though.
Yes, my application only has one child form, but your suggestion when
writing an MDI app with multiple child forms is also appreciated.
>

There may have been a simpler way to do it, but it has the distinct
property of working.

That is always good :-)
I think the way I have found is much simpler though
All you needed to do was handle the MDIChildActivate event, check if
MdiChildren.Length=0, and update your toolbar as appropriate.
In my case, only the child form needed to know if it was the last
child window being closed, but since the proeprty that defines the
current number of open child windows was in the Main Form, any code in
the Main Form had access to it as well.

In my case none of my child forms, need to know whether they are the last
form or not. Much simpler, I think.

Aug 7 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.