Introduction
This article will demonstrate an easy and effective way to include MDI support in your Windows Forms Applications (.NET)! There are many examples on MSDN, but none of them puts together a sample program that is all in one place. Adding MDI support to your application gives you the advantage of managing multiple files or some other form of data with one template. This will dramatically reduce the amount of coding that you have to do, and will allow end users the comfort of working with an application that gives them the power to work with tons of data all at once. This article will show you how to setup a basic application with MDI support that uses a RichEdit
control to provide a text editor.
Using the code
In the source zip, you will find this entire project with all the steps completed below.
First of all, this article is written mainly for Microsoft Visual C++ .NET 2003 users. There is no guarantee that this code will work with previous or future versions! Let's start off by opening a blank Microsoft Forms Application (.NET) and naming it MDIApp.
Click OK, and you will be presented with a blank form named Form1
. Because it will require you to modify many lines and filenames, I will not go through changing this name, but if you decide to change Form1
to something different, you must substitute Form1
for whatever you decided to name it for the rest of this article.
Next, click the form and open the Properties window. Under ismdicontainer
change the value to true
. This will tell the compiler that we want this to be our MDI parent. If you wish, you can change other values to make the form appear like you want. I recommend changing WindowState
to Maximize
to produce a full screen window.
Now that we have our MDI container, we need to add a menu. Drag a MainMenu
control from the toolbar onto your form. Create a top-level menu item with the Text
property set to &File, with submenu items called &New and E&xit. Also create a top-level menu item called &Window. Select the menu item that corresponds to the &Window menu item, and set the MdiList
property to true
. This will tell our application that we want it to append child windows at the end as a submenu under the &Window menu.
Now that we have our parent window, it's time to create a child window. In the Solution Explorer, right-click the project, point to Add, and then select Add New Item. In the Add New Item dialog box, select Windows Forms Application (.NET) from the Templates pane. In the Name box, name the form Form2 (again, if you change this, you will have to substitute). Click the Open button to add the form to the project. Form2
will open by default. This form will be the template for your MDI child forms. Even though you will have the option of changing the Opacity
property on your child window, do not do it! This will cause painting problems to occur.
Drag a RichTextBox
control from the Toolbox to the form. In the Properties window, set the Anchor
property to Top, Left
and the Dock
property to Fill
. This will make our RichTextBox
fill up the entire child window! Create a Click
event handler for the New menu item. This is best done by double clicking the New sub-item on the menu (in design mode).
Add the following code under our new function that was created:
private: System::Void menuItem2_Click(System::Object * sender,
System::EventArgs * e)
{
Form2* newMDIChild = new Form2();
newMDIChild->MdiParent = this;
newMDIChild->Show();
}
That will create a new child window with the design that we made. Scroll to the top and add the line below...
#include "Form2.h"
Go back to our design view and create a click event handler for the Exit menu item.
Add the following code under our new function that was created for the Exit menu item:
private: System::Void menuItem3_Click(System::Object * sender,
System::EventArgs * e)
{
this->Close();
}
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.
Points of Interest
Before I learned about MDI for .NET, I was forced to use Microsoft's alternative method in making my program MFC. This wasn't useful for me because I am new to MFC and don't know many functions. Also, it is useful to add XP support so that people with XP will see a cleaner design. It will also help to add a status bar to fill the missing space at the bottom of the window. The last thing is something in my preference but it is recommended that you set your child window to Maximize
for WindowState
for better appearance.
History
So far this is the first article and first version of this article that I have made! ;-)