Click here to Skip to main content
15,868,159 members
Articles / Desktop Programming / Windows Forms
Article

Adding MDI Support to Windows Forms Applications (.NET)

Rate me:
Please Sign up or sign in to vote.
3.70/5 (15 votes)
6 Mar 20064 min read 102.1K   1.8K   21   14
An article on adding MDI support to .NET applications.

Final Application

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.

Step 1

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.

Step 2

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:

MC++
private: System::Void menuItem2_Click(System::Object *  sender, 
                                      System::EventArgs *  e)
{
    // This gets a new instance of our child window.
    Form2* newMDIChild = new Form2();
    // Tells our new child window
    // what is our parent window.
    newMDIChild->MdiParent = this;
    // Shows the new form.
    // Don't confuse this with ShowDialog!
    newMDIChild->Show();
}

That will create a new child window with the design that we made. Scroll to the top and add the line below...

MC++
#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:

MC++
private: System::Void menuItem3_Click(System::Object *  sender, 
                                      System::EventArgs *  e)
{
    this->Close(); // Closes the application!
}

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! ;-)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralTitle change Pin
R. Senthil Kumaran18-Oct-08 21:32
R. Senthil Kumaran18-Oct-08 21:32 
GeneralRe: Title change Pin
Gabriel Graves18-Oct-08 21:35
Gabriel Graves18-Oct-08 21:35 
GeneralProblem Pin
C# Beginner Nick25-Aug-08 16:54
C# Beginner Nick25-Aug-08 16:54 
GeneralVisual 2005 Express Edition Pin
SzyKam17-Aug-06 21:05
SzyKam17-Aug-06 21:05 
GeneralRe: Visual 2005 Express Edition Pin
Gabriel Graves18-Aug-06 17:34
Gabriel Graves18-Aug-06 17:34 
QuestionExtention? Pin
Dr. Pastor10-Apr-06 10:14
Dr. Pastor10-Apr-06 10:14 
AnswerRe: Extention? Pin
Gabriel Graves10-Apr-06 11:07
Gabriel Graves10-Apr-06 11:07 
GeneralRe: Extention? Pin
Dr. Pastor11-Apr-06 6:44
Dr. Pastor11-Apr-06 6:44 
AnswerRe: Extention? Pin
Gabriel Graves11-Apr-06 12:59
Gabriel Graves11-Apr-06 12:59 
GeneralRe: Extention? Pin
Kai Wallenda30-May-06 7:13
Kai Wallenda30-May-06 7:13 
GeneralRe: Extention? Pin
Dr. Pastor30-May-06 11:17
Dr. Pastor30-May-06 11:17 
QuestionIn VB.NET? Pin
Sheshadrinath14-Mar-06 17:29
Sheshadrinath14-Mar-06 17:29 
AnswerRe: In VB.NET? Pin
Gabriel Graves15-Mar-06 11:10
Gabriel Graves15-Mar-06 11:10 
General"D" in MDI... Pin
Jun Du6-Mar-06 12:20
Jun Du6-Mar-06 12:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.