Export (0) Print
Expand All

Creating MDI Child Forms

Visual Studio .NET 2003

An essential element of Multiple-Document Interface (MDI) Applications is MDI child forms, as these are the center of user interaction.

In the following procedure, you will create MDI child forms that display a RichTextBox control, similar to most word-processing applications. Substituting the RichTextBox control with other controls, such as the DataGrid control, or a mixture of controls allows you to create MDI child windows (and, by extension, MDI applications) with diverse possibilities.

To create MDI child forms

  1. Create an MDI parent form that has a menu structure containing File and Window top-level menu items and New and Close menu items. For more information on creating MDI parent forms, see Creating MDI Parent Forms.
  2. In the drop-down list at the top of the Properties window, select the menu item that corresponds to the &Window menu item and set the MdiList property to true.

    This will enable the Window menu to maintain a list of open MDI child windows with a check mark next to the active child window.

  3. In Solution Explorer, right-click the project, point to Add, and then select Add New Item.

    This form will be the template for your MDI child forms.

    Note   The MDI child form you create in this step is a standard Windows Form. As such, it has an Opacity property, which allows you to control the transparency of the form. However, the Opacity property was designed for top-level windows. Do not use it with MDI child forms, as painting problems can occur.
  4. In the Add New Item dialog box, select Windows Form (in Visual Basic or in Visual C#) or Windows Forms Application (.NET) (in Visual C++) from the Templates pane. In the Name box, name the form Form2. Click the Open button to add the form to the project.

    The Windows Forms Designer opens, displaying Form2.

  5. Drag a RichTextBox control from the Toolbox to the form.
  6. In the Properties window, set the Anchor property to Top, Left and the Dock property to Fill.

    This causes the RichTextBox control to completely fill the area of the MDI child form, even when the form is resized.

  7. Create a Click event handler for the New menu item. For more information about creating event handlers, see Creating Event Handlers on the Windows Forms Designer.
  8. Insert code similar to the following to create a new MDI child form when the user clicks the New menu item (In the example below, the event handler handles the Click event for MenuItem2. Be aware that, depending on the specifics of your application architecture, your New menu item may not be MenuItem2.)
    ' Visual Basic
    Protected Sub MDIChildNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
       Dim NewMDIChild As New Form2()
       'Set the Parent Form of the Child window.
       NewMDIChild.MdiParent = Me
       'Display the new form.
       NewMDIChild.Show()
    End Sub
    
    // C#
    protected void MDIChildNew_Click(object sender, System.EventArgs e){
       Form2 newMDIChild = new Form2();
       // Set the Parent Form of the Child window.
       newMDIChild.MdiParent = this;
       // Display the new form.
       newMDIChild.Show();
    }
    
    // C++
    private:
       System::Void menuItem2_Click(System::Object *  sender,
          System::EventArgs *  e)
       {
          Form2 *newMDIChild = new Form2();
          // Set the Parent Form of the Child window.
          newMDIChild->MdiParent = this;
          // Display the new form.
          newMDIChild->Show();
       }
    

    In Visual C++, add the following #include directive at the top of Form1.h:

    // C++
    #include "Form2.h"
    
  9. Press F5 to run the application. Note that by selecting New from the File menu, you can create new MDI child forms, which are kept track of in the Window menu.
    Note   Be aware that when an MDI child form that has a MainMenu component (with, usually, a menu structure of menu items) and it is opened within an MDI parent form that has a MainMenu component (with, usually, a menu structure of menu items), the menu items will merge automatically if you have set the MergeType property (and optionally, the MergeOrder property). Set the MergeType property of both MainMenu components and all of the menu items of the child form to MergeItems. Additionally, set the MergeOrder property so that the menu items from both menus appear in the desired order. For more information about using the MergeOrder property to determine the position of menu items, see Merging Menu Items Programmatically.
    Note   Keep in mind that when closing an MDI parent form, each of the MDI child forms raises a Closing event before the Closing event for the MDI parent is raised. Cancelling an MDI child's Closing event will not prevent the MDI parent's Closing event from being raised; however, the CancelEventArgs argument for the MDI parent's Closing event will now be set to true. You can force the MDI parent and all MDI child forms to close by setting the CancelEventArgs argument to false.

See Also

Multiple-Document Interface (MDI) Applications | Creating MDI Parent Forms | Determining the Active MDI Child | Sending Data to the Active MDI Child | Arranging MDI Child Forms

Show:
© 2014 Microsoft