MDI Form and MDI Children Question

jango_fett

Member
Joined
Jun 15, 2005
Messages
23
Programming Experience
Beginner
[RESOLVED] MDI Form and MDI Children Question

Hi, all. I'm a newcomer to VB .NET and I'm having a hard time with MDI forms. Specifically, whenever the user attempts to close the MDI form, I need to make sure that all of its children forms are closed. If they are not, I display a message asking the user if they really want to leave.

For example, I state the child form like this:
VB.NET:
Friend WithEvents MyChild as frmMDIChild
To open it, the code goes:
VB.NET:
MyChild = New frmMDIChild
MyChild.MdiParent = Me
MyChild.Show()
The following code goes in the variable's Closed event:
VB.NET:
Private Sub MyChild_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyChild.Closed
      MyChild = Nothing
End Sub
Then, I code in the MDI form Closing event:
VB.NET:
Private Sub frmMDI_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
      If MyChild Is Nothing = False Then
            if MsgBox("There are still open windows. Really exit?", _
MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Exit") _ 
= MsgBoxResult.No Then
            e.Cancel = True
      End If
End Sub
The above code seems to work, but the thing is that I have many children forms and to validate each one of them is overkill. Is there a better way to reference them all at once? Any help would be appreciated. Many thanks!:)
 
Last edited:
Well, that is the answer to the question you asked... if there are MdiChildren of a form then MdiChildren.Length is greater than zero.
 
Thanks, JohnH!

Excellent! Thanks, JohnH. I tried out the code shown in the link you provided, but it didn't work the first time. My fault, obviously. But now it did! That was all I needed, thanks. I really appreciate it. You were a great help to me today. :)
 
Back
Top