This documentation is archived and is not being maintained.

Creating a Window List for an MDI Form

Visual Studio .NET 2003

Among the different Windows Forms layout options is Multiple Document Interface (MDI). A handy way to keep track of the different MDI child windows an application has open is with a Window list. The functionality to keep track of all the open MDI child forms as well as which has focus is part of the Visual Studio .NET framework, and is achieved through setting the MDIList property of a menu item.

To create a Window list for an MDI form

  1. In the Properties window, set the IsMDIContainer property of the MDI form to true.
  2. In Solution Explorer, right-click the project, click Add, and then select Add New Item. In Visual Basic or in Visual C#, select Windows Form. In Visual C++, select Windows Forms Application (.NET). Name this form Form2.
  3. From the Toolbox, add a MainMenu component to your MDI parent form. For more information about adding controls to forms, see Adding Controls to Windows Forms.
  4. Click the MDI parent form to give it focus. In the Properties window, set the Menu property to MainMenu1 (in Visual Basic), or mainMenu1 (in C# or in C++).
  5. Add the following top-level menu items to the MainMenu component using the Menu Designer.
    Menu ItemText
    MenuItem1&File
    MenuItem2&Window

    For more information about adding menu items to a MainMenu component, see Adding Menus and Menu Items to Windows Forms.

  6. In the Menu Designer, add a submenu item (MenuItem3) to MenuItem1. Set its Text property to &New.

    For more information about adding submenu items to menu items, see Adding Menus and Menu Items to Windows Forms.

  7. In the Properties window, select MenuItem2 from the drop-down list. Set the MDIList property to true.
  8. Create an event handler for the Click event for MenuItem3. For more information on creating events, see Creating Event Handlers on the Windows Forms Designer.
  9. Within the event handler, insert code similar to the following to create and display new instances of Form2 as MDI children of Form1:
    'Visual Basic
    Protected Sub New_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
       'Create a new instance of Form2
       Dim NewMDIChild As New Form2()
       'Set the parent of the MDI child form.
       NewMDIChild.MDIParent = Me
       'Display the new form.
       NewMDIChild.Show()
    End Sub
    
    //C#
    protected void New_Click(object sender, System.EventArgs e)
    {
       //Create a new instance of Form2.
       Form2 NewMDIChild = new Form2();
       //Set the parent of the new MDI child form.
       NewMDIChild.MDIParent = this;
       //Display the new MDI child form.
       NewMDIChild.Show();
    }
    
    Note   Be sure that the necessary code to enable the event handler is present. In this case, it would be similar to the following:
    this.menuItem3.Click += new System.EventHandler(this.New_Click);
    // C++
    private:
       System::Void menuItem3_Click(System::Object *  sender,
          System::EventArgs *  e)
       {
          //Create a new instance of Form2.
          Form2 *NewMDIChild = new Form2();
          //Set the parent of the new MDI child form.
          NewMDIChild->MdiParent = this;
          //Display the new MDI child form.
          NewMDIChild->Show();
       }
    

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

    // C++
    #include "Form2.h"
    
  10. Press F5 to run the application. From the File menu, select New to create a new MDI child form. Note that the Window menu always displays a list of the MDI child forms open within the application, with a check mark next to the MDI child that has focus.

See Also

Introduction to the Windows Forms MainMenu Component | Menus in Windows Forms | What's New in Windows Forms and Controls

Show: