How To - Create MDI Child Forms - Microsoft Docs
How To - Create MDI Child Forms - Microsoft Docs
In this article
See Also
In the following procedure, you will create MDI child form that displays a
RichTextBox control, similar to most word-processing applications.
Substituting the System.Windows.Forms control with other controls, such as
the DataGridView control, or a mixture of controls enables you to create MDI
child windows (and, by extension, MDI applications) with diverse possibilities.
Note
The dialog boxes and menu commands you see might differ from those described
in Help depending on your active settings or edition. To change your settings,
choose Import and Export Settings on the Tools menu. For more information, see
Customizing Development Settings in Visual Studio.
2. From the Toolbox , drag a MenuStrip control to the form. Set its
Text property to File.
3. Click the ellipses () next to the Items property, and click Add to add
two child tool strip menu items. Set the Text property for these
items to New and Window.
5. 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.
Note
The MDI child form you created in this step is a standard Windows Form. As
such, it has an Opacity property, which enables 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.
This form will be the template for your MDI child forms.
7. 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.
8. Double click the New menu item to create a Click event handler for it.
9. Insert code similar to the following to create a new MDI child form
when the user clicks the New menu item.
Note
In the following example, 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 .
VB Copy
C# Copy
C++ Copy
private:
void menuItem2_Click(System::Object ^ sender,
System::EventArgs ^ e)
{
Form2^ newMDIChild = gcnew 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++ Copy
#include "Form2.h"
10. In the drop-down list at the top of the Properties window, select the
menu strip that corresponds to the File menu strip and set the
MdiWindowListItem property to the Window ToolStripMenuItem.
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.
11. Press F5 to run the application. By selecting New from the File menu,
you can create new MDI child forms, which are kept track of in the
Window menu item.
Note
See Also