How to Use the Forms Collection to Unload All MDI Child Forms

Last reviewed: June 21, 1995
Article ID: Q97620
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0

SUMMARY

You can use Visual Basic code to close all MDI children by using the forms collection. The forms collection contains references to all forms -- the MDI parent form, MDI children forms, and non-MDI forms. To unload or close all MDI forms, loop through the forms collection testing the value of the MDIChild property on each form. If the MDIChild property is true, unload the form.

MORE INFORMATION

Steps to Create Example

1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N)
   if Visual Basic is already running. Form1 is created by default.

  • Change the MDIChild property of Form1 to True.

  • From the File menu, choose New MDI Form (Alt+F+I). This creates the MDIForm1 MDI parent form.

  • From the Window menu, choose Menu Design (ALT+W+M), and create the following menu with two menu items on MDIForm1:

       Caption     Name         Indent
       ------------------------------
       File        mFile       no
       New         mNew        once
       Close All   mCloseAll   once
    
    

  • Add the following code to the general declarations section of MDIForm1.

    Dim ChildCount As Integer

  • Add the following code to the mNew event handler.

       Sub mNew_Click ()
          Dim newWindow As New Form1
          ChildCount = ChildCount + 1
          newWindow.Caption = "Child " & Str$(ChildCount)
          newWindow.Show
       End Sub
    
    

  • Add the following code to the mCloseAll_Click event handler.

       Sub mCloseAll_Click ()
          i = 1
          Do While i < Forms.Count
             If forms(i).MDIChild Then
                ' *** Do not increment i% since a form was unloaded
                Unload forms(i)
             Else
                ' Form isn't an MDI child so go to the next form
                i = i + 1
            End If
          Loop
          ChildCount = 0
       End Sub
    
    

  • From the Options menu, choose Project. Make MDIForm1 the Start Up Form.

  • From the Run menu, choose Start (ALT, R, S) to run the program.

  • From the File menu on MDIForm1, choose New. Repeat this several times.

  • From the File menu on MDIForm1, choose Close All to unload all the MDI

        children.
    


  • Additional reference words: 2.00 3.00
    KBCategory: kbprg kbcode
    KBSubcategory: PrgCtrlsStd


    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

    Last reviewed: June 21, 1995
    © 1998 Microsoft Corporation. All rights reserved. Terms of Use.