Form Count in MDI App


G

Guest

Hi

Ok here goes I have an MDI application and when a user clicks on the close button on the form "the X" , I would like to disable menus if there are no more MDI Chid forms. So I decided to raise an event from the MDI Child form to the parent, allowing the parent to disable menus if it has no more MDI Child Forms. This works fine if I only create one child form but when there are anymore then that the count shows that there is one more then should be untill the GC comes to get it
I have tried raising this event from the closing, closed and disposed events of the Child form

Does anybody have any idea how I can force an accurate count of MDI Child forms

Here is the code

Dim frm As For
Dim Count As Intege
For Each frm In Me.MdiChildre
Count = Count +
Nex
If Not mnuReportClose.Enabled = True The
mnuReportClose.Enabled = Tru
End I
If Not mnuWindow.Enabled = True The
mnuWindow.Enabled = Tru
End I
If Count < 1 The
mnuReportClose.Enabled = Fals
mnuWindow.Enabled = Fals
End I

Thanks
 
Ad

Advertisements

C

CJ Taylor

on your MDI Parent

Me.MdiChildren.Length

Chris Lane said:
Hi,

Ok here goes I have an MDI application and when a user clicks on the close
button on the form "the X" , I would like to disable menus if there are no
more MDI Chid forms. So I decided to raise an event from the MDI Child form
to the parent, allowing the parent to disable menus if it has no more MDI
Child Forms. This works fine if I only create one child form but when there
are anymore then that the count shows that there is one more then should be
untill the GC comes to get it.
 
G

Guest

Hi CJ

When I raise the event from the child form that runs the code I posted before with 2 child forms created here is what
happens
Click the close butto
Raise the event from Closing or Closed the length =
Raise the event from Disposed event seems to work fine until I raise the event from the last active child form the event never completes execution, I believe because the GC removed the last child form obeject before the process can complete. So the problem is with the last active child form I can't seem to raise the event to disable the menus in the parent form

I thought of sending the event handler in the parent a 1 and subtracting that from length then I would have the right count
But I really don't like that idea much

Thanks

Chris
 
G

Guest

Also when I am canceling the closing of the child form, the last active child form ignores this and still disposes before completing the event

Private Sub frmReport_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closin
e.Cancel = Tru
m_CallingForm = Nothin
RaiseEvent BaseReportClosed(1
e.Cancel = Fals
End Su

Please help.
 
Ad

Advertisements

C

CJ Taylor

There is some weird stuff with the Form.Closing event. I have had a lot of
problems with its behavior.

Also, your e.Cacnel = true does absolultly nothing in this case. As for
being disposed before completing the event I'm not sure how that happens, an
event subscriber (unless performing an asynchronous call) must complete its
operation and release control back to the event itself calling the next
event in the chain

So unless your calling something from one of your subscribers on a separate
thread, then I'm not sure how thats happening. However, that doesn't mean
your wrong, there is a lot of "kludge" behavior in .NET that straight up
pisses me off. (i.e. the control.validated event firing before the
form.closing [email protected][email protected])



Chris Lane said:
Also when I am canceling the closing of the child form, the last active
child form ignores this and still disposes before completing the event.
Private Sub frmReport_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
 

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

Top