Detect when no mdi child windows displayed

  • Thread starter Thread starter Phil
  • Start date Start date
P

Phil

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.
 
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 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
 
I can check for MdiChildren.Length=0, but which event handler should I put
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.
 
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.
 
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
 
Phil said:
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.
 
"Phil" <N/A> wrote in message



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
 
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.
 
Hi Phil,

I also think that you'd better subscribe and handle the FormClosed event of
the MDI child forms to get notified when an MDI child form is closed.

In VB.NET, to subscribe an event at run time, we need to use the AddHandler
statement. For more information on the AddHandler statement, you may read
the following MSDN document:

http://msdn2.microsoft.com/en-us/library/7taxzxka(VS.80).aspx

The following is a sample code. Note that when the last MDI child form is
being closed and the FormClosed event of the MDI child form is fired, the
MDI parent's MdiChildren.Length property still returns 1.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim frm1 As New Form
frm1.MdiParent = Me
AddHandler frm1.FormClosed, AddressOf MdiChildClosed
Dim frm2 As New Form
frm2.MdiParent = Me
AddHandler frm2.FormClosed, AddressOf MdiChildClosed
Dim frm3 As New Form
frm3.MdiParent = Me
AddHandler frm3.FormClosed, AddressOf MdiChildClosed
frm1.Show()
frm2.Show()
frm3.Show()
End Sub

Private Sub MdiChildClosed(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosedEventArgs)
' At this time, the MDI parent's MdiChildren.Length still returns 1
If (Me.MdiChildren.Length = 1) Then
MsgBox("No Mdi clild form is opened")
End If
End Sub

Hope this helps.
If you have any question, 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.
 
rowe_newsgroups <[email protected]>'s wild thoughts were
released on Wed, 01 Aug 2007 09:41:07 -0700 bearing the
following fruit:
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.
 
rowe_newsgroups said:
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.
 
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.
 
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.
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.




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.
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.
 
Back
Top