Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keeping Track of MDI Children

Status
Not open for further replies.

miket26

Programmer
Apr 13, 2004
63
CA
I have an MDI application, and I want to raise an event within the parent whenever one of its mdi child forms are closed. Basically, if there are no mdi child forms left, I want to perform several actions. It is such a seemingly simple problem, but I can't figure out a solution.

Thanks.
 
Make an event handler in your main form for Closing, or Closed, whichever applies, and hook your child form's events to it.
 
Here's an idea create a integer variable for the form that stores the number of forms you have.

Then when you open a form increment the variable, and add a handler to the new forms closing/closed event to capture when the form closes.

Then in the handler sub, decrement the variable and check if it is zero.

Another option - untested - would be this. You still need to add the handler whenever you open the form, but in the closing/closed event check then Me.OwnedForms property - it returns a collection which you should be able to count to find the number of forms open. Depending on where you put it (closing, closed) it may still show 1 as being Owned if it is still in the process of closing.
 
Actually a better option than OwnedForms is available - try MdiChildren instead.
 
Thanks for your input. I got thinking about how to use an existing event, since I don't know how to add my own. I created a class variable f of type Form and created the following 2 methods:


Private Sub me_mdiChildClose(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.MdiChildActivate
f = Me.ActiveMdiChild
End Sub
Private Sub f_closing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles f.Closed
rptCount = rptCount - 1

If rptCount = 0 Then
'do stuff
End If
End If
End Sub
 
I would get rid of the whole me_mdiChildClose, and instead just use an AddHandler statement after you initially declare your child forms. That way, the event can process all of the mdi child forms instead of just being hooked to one instance.
 
I've just tried this and it seems to work:

Form1 is the MDI parent
Form2 is the MDIChild

Code:
FORM1:
  Dim ChildCount As Integer = 0

  Public Sub ChildClosed()

    ChildCount -= 1
    If ChildCount = 0 Then
      Button2.Enabled = True
      Button2.Focus()
    End If

  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Button2.Enabled = False
    ChildCount += 1
    Dim f2 As New Form2(Me)
    f2.MdiParent = Me
    f2.Text = "Form2: " + ChildCount.ToString
    f2.Show()

  End Sub


  Private Sub Button2_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Enter

    MessageBox.Show("Here!!!")

  End Sub

  Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

    MessageBox.Show("Button2 clicked")

  End Sub

Code:
FORM2:

  [b]Public Sub New([u][i]ByVal parentform As Form1[/i][/u])
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    [u][i]MyParent = parentform[/i][/u]

    'Add any initialization after the InitializeComponent() call

  End Sub[/b]

rest of WindowsForms region 

then

  Private MyParent As Form1

  Private Sub Form2_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Disposed

    MyParent.ChildClosed()

  End Sub

Hope this help.
 
I did the addHandler() statement, and its works o.k. now. Thanks for all your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top