Geekpedia Programming Tutorials






Using MDI

Covers the creation of child forms at runtime inside a MDI form and other programming aspects, like building menus.

On Monday, May 10th 2004 at 05:17 PM
By Andrew Pociu (View Profile)
*****   (Rated 4.3 with 42 votes)
Contextual Ads
More C# Resources
Advertisement

Applications like Microsoft Word or even Microsoft Visual Studio use MDI (Multiple Document Interface) forms. Named so because of their main characteristic - holding multiple other forms (documents) like a container.
It's not neccesary to discuss more about this as you already know MDI from you day-to-day use.

Create a new 'Windows Application' project in Microsoft Visual C# .NET named 'MDI' (or whatever you want).
In the properties of the newly created form set the 'IsMdiContainer' property to true. This will make the form a MDI form just as we want it.

How can we create child forms at runtime?

Create a new menu 'mainMenu1' by double-clicking 'MainMenu' from the Toolbox.
Create two menu items named 'File' and 'Windows'. Name them 'mnuFile' and 'mnuWindows'.
Set the 'MdiList' property of the 'mnuWindows' menu to 'True'. This way in this menu the currently opened child forms will be displayed.
Create two items in the File menu: 'New' and 'Exit'. Name them 'mnuFileNew' and 'mnuFileExit'.
For the item 'New' create the sub-items 'Standard', 'Blue' and 'Red'. Name them 'mnuFileNewStandard', 'mnuFileNewBlue' and 'mnuFileNewRed'.

The hierarchy for the File menu should look like this.

File
->New
-->Original
-->Blue
-->Red
->Exit

It's better to understand with a screenshot:



As you know from Microsoft Word and all the other MDI applications, the creation of new child forms is made from a menu or a toolbar. In this tutorial we use a menu.

We choosed in this tutorial to be able to build 3 types of child forms, 'Original', 'Blue' and 'Red'.
Let's see how we can create a child form at runtime, as this is the main scope of the tutorial.

Double click the 'Original' item from the File -> New menu.
Visual C# .NET sends us to the part where we code:


private void mnuFileNewStandard_Click(object sender, System.EventArgs e)
{

}



Replace the code with the following:


private void mnuFileNewStandard_Click(object sender, System.EventArgs e)
{
    // Create a new form
    Form standardFrm = new Form();

    // Give it a title
    standardFrm.Text = "Standard Form";

    // Who's child is this window
    standardFrm.MdiParent = this;

    // Show it
    standardFrm.Show();
}



In the first line we create a new form named 'standardFrm'. The new form is created like any other form. In the second we give it a generic title.
In the third we specify the parent of the newly created window by setting MdiParent to the current main window (this). This makes the form appear in the MDI form.
Finally the form is displayed.

It's time to make the blueFrm. Double click the 'Blue' item from the File -> New menu.
After Visual C# .NET takes you to the part where you code, use the following code:


private void mnuFileNewBlue_Click(object sender, System.EventArgs e)
{
    // Create a new form
    Form blueFrm = new Form();

    // Give it a title
    blueFrm.Text = "Blue Form";

    // Change the background color of the form
    blueFrm.BackColor = System.Drawing.Color.Blue;

    // Make the child form maximized
    blueFrm.WindowState= System.Windows.Forms.FormWindowState.Maximized;

    // Who's child is this window
    blueFrm.MdiParent = this;

    // Show it
    blueFrm.Show();
}




Again we create a new form, this time named 'blueFrm' and we give it a title.
We set the background color of the new form to blue in the third line of code.
Next we set the WindowsState of the Window to Maximized.
We assign the window to the parent window (MDI) and display it.



After you compile the code create a new Standard form and a Blue form.
As you saw, the Blue form is maximized. Click the 'Windows' menu and you will see the list of available opened forms and a checkmark next to the one selected. Using this menu you can easily switch between forms.

Finally, let's make the code for redFrm by double clicking the 'Red' item from the File -> New menu and inserting this code:


private void mnuFileNewRed_Click(object sender, System.EventArgs e)
{
    // Create a new form
    Form redFrm = new Form();

    // Give it a title
    redFrm.Text = "Red Form";

    // Change the background color of the form
    redFrm.BackColor = System.Drawing.Color.Red;

    // Create a new button
    Button newStandardBtn = new Button();
    
    // Set the width of the button
    newStandardBtn.Width = 120;

    // Set the caption of the button
    newStandardBtn.Text = "New Standard form";

    // Set the position of the button
    newStandardBtn.Left = (redFrm.Width - newStandardBtn.Width) / 2;
    newStandardBtn.Top = 20;

        // When the button is clicked create a Standard form
    newStandardBtn.Click += new System.EventHandler(this.mnuFileNewStandard_Click);

    // Add the button to the form
    redFrm.Controls.Add(newStandardBtn);

    // Who's child is this window
    redFrm.MdiParent = this;

    // Show it
    redFrm.Show();
}



As you can see, the first lines do the same thing as the earlier examples.
On the next lines we create a button named 'newStandardBtn' and give it a width of 120 pixels. Next we set this button's Text property to 'New Standard form' - because it will create a new Standard form.
We horizontally center the button by finding the center of the form (substract the width of the button from the form and divide the result by 2) and set the vertical position 20 pixels far from the top.

When the Click event of the button occurs, that is when the user clicks the button we want to act the same as if he was clicking the Standard item from the File -> New menu.
After that the button is added to the form and we're finished!

The following is a screenshot of our brand new ugly red form:



If you want to add functionality to the 'Exit' item in the 'File' menu add the following code after double clicking on it:


private void mnuFileExit_Click(object sender, System.EventArgs e)
{
    Application.Exit();
}
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by saji on Wednesday, February 9th 2005 at 01:38 AM

This document is very good as iam a beginner in creating mdi in vs.net.
Thank you very much......

by karthikeyan ramchel on Sunday, February 20th 2005 at 11:37 PM

Very useful to know the about MDI forms

by arshad on Sunday, October 23rd 2005 at 03:19 AM

Thanks for information great keep going.....

by Karsus on Tuesday, August 29th 2006 at 04:22 PM

By the dark forces of hell, that's exactly what i was looking for.

by Siva Subramanian on Wednesday, September 13th 2006 at 02:53 AM

hello sir,

thank u, i use this program its working
but i want to open child forms thats i created
but what the problem is if i click open 1 child frm it open then i click simuntaneusly it also open
but what i want to open only 1 child frm at a time

by Pawan Chand on Wednesday, March 7th 2007 at 06:11 PM

Hi this program is really beneficial for all specially who is a beginner of using MDI form ..

by Alfonso Calderon on Tuesday, June 26th 2007 at 10:12 AM

Hi, good day, I have a small question.

Do you know why the menu is duplicating when I open a form?

Thanks and best regards

AC

by Jayson on Monday, October 22nd 2007 at 05:19 PM

Hi, I just a have a question regarding MDI and I was wondering if you can help me. I have multiple child mdi and when I maximize a mdi form and open another child mdi the new form is also maximized. Is there a way to keep the size of the new form even the other form is maximized?
Thanks

JC

by aries on Saturday, March 22nd 2008 at 01:28 AM

i've same problem as faced by JC .. can anyone help !

by saranya on Thursday, April 17th 2008 at 03:08 AM

how give the same color for mdiform and form1? plz provide the coding for my mail
how to design the mdiform in vb(.net)?

by Teryaki on Sunday, May 11th 2008 at 04:20 PM

I followed th instruction and i was happy about the results. Thanx for the nice tutorial.

I tried to make a docked RichTextBox, i hope i wrote it correct. It works any way :D

private void mnuFileNewTextArea_Click(object sender, EventArgs e)
{
Form textAreaFrm = new Form();
textAreaFrm.Text = "Text Form";

//Create RichTexrBox
RichTextBox newRtb = new RichTextBox();
newRtb.Dock = System.Windows.Forms.DockStyle.Fill;
textAreaFrm.Controls.Add(newRtb);
textAreaFrm.MdiParent = this;
textAreaFrm.Show();
}

by Rirya on Thursday, September 4th 2008 at 10:56 PM

i have a question ... if i have the parent and i opened a child and closed it again how can i use the properties of the menustrip of the parent? i have to control de enabled property of the menustrip but i don't know how to do it after i close the child (childfrm.close();), thank you

by vinoth on Sunday, September 7th 2008 at 02:00 AM

I am having one probel with using mdiforms. That is when i create a multiple child, the child forms are opening one after another. I just want to open one form at a time. Only after closing a child form another child form has to be open. This is my problem. Can you solve this for me?
Thenk you

by vinay kasra on Tuesday, December 29th 2009 at 02:20 PM

Hello, I am Vinay Kasera from Kanpur,UP,India,
and I have a problem in C#.Net which is "I want to open a mdi Child form in a part of MDI Parent form which is divided in three part.
now the first part use a header of project and second use as the menu option which has linkbuttons and third remaining part used for working.
And I hope u send me answerif you have.
Thanks.


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons