Skip to main content

MDI Forms in C#

Introduction to MDI Forms with C#

By b, 25  nov  2012
Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again. Alternatively, you can update your subscriptions.

Introduction

This article tries to explain how to develop an MDI application. Text editors and image editors are common examples where multiple documents are useful. The attached demo application allows you to open images in child windows and then watermark them with a custom text.

Using the code

Creating parent window

Add a new form to your application. To make this form as the parent form, set its IsMDIContainer property to true. A MainMenu can be added to the parent form, which will have options to open a new child form, close a child form, switch between child forms and change layout style of child forms.

Creating child form template

Add a second form which will be the template for the child forms. Each time you want to create a new child window to your application, you can create a new instance of this template form and make the first form as its parent form.
//Create a new instance of the MDI child template form
Form2 chForm = new Form2(); 

//Set parent form for the child window 
chForm.MdiParent=this;

//Display the child window
chForm.Show();

Merging menus of child and parent

When we have a MainMenu for both the parent and child form(s), we can merge the menus if we want to do so. This is done by setting the MergeType property of the MenuItem(s) to MergeItems. In the sample, I wanted the File menu of the child and the parent to merge. So I set the MergeType property to MergeItems for the File MenuItem of the child and the parent forms. In addition, we can utilize the MergeOrder property of the MenuItem(s) to set the relative position of each MenuItem, when menus are merged. In my sample application, I have set the MergeOrder property of the New MenuItem in the parent form to 0 and that of the Close MenuItem to 4. The MenuItems in the child form has MergeOrder values from 1 to 3 so that those MenuItems appear between File and Close MenuItems of the parent form, after merge.
The MergeType property can be assigned any of the following four values:
ValueMeaning
AddA MenuItem is added to the combined set of MenuItems while merging menus
RemoveA MenuItem is not included when menus are merged
MergeItemsWhen you have submenu items to a MenuItem, use this value to merge the items to the combined menu
ReplaceThis value is used when a MenuItem should replace another MenuItem in the same position in a merged menu.

Changing layout of child windows within the parent

To change the layout of child forms within a parent form use LayoutMdi method of the parent form. Child forms can be tiled horizontally, vertically, cascaded or as icons within the parent form. MdiLayout enumeration has four values which can be supplied as arguments to the LayoutMdi method to achieve any of the four layouts.
ValuePurpose
ArrangeIconschild window icons are arranged within the parent
Cascadearrange the child windows within the parent window in a cascaded fashion
TileHorizontaltile the child windows horizontally
TileVerticaltile the child windows vertically
//Cascade all child forms.        
this.LayoutMdi(System.Windows.Forms.MdiLayout.Cascade);

Minimize all and Maximize all

The MdiChildren property of a form gives the array of child forms which belong to the parent form. To maximize or minimize all child windows together, all we need to do is to loop through this array of child forms and set the WindowState property of each child form to the proper value in FormWindowState enumeration.
//Gets forms that represent the MDI child forms 
//that are parented to this form in an array 
Form[] charr= this.MdiChildren; 
     
//For each child form set the window state to Maximized 
foreach (Form chform in charr) 
   chform.WindowState=FormWindowState.Maximized;
If we loop through the child forms and call the Close method of each, we will get a "Close all child forms" feature.

Adding scroll for large images

The child form template has a Panel and a PictureBox control. A Panel is a container control. And if its AutoScroll property is set to true, any controls contained within it can be scrolled. I have set the SizeMode property of PictureBox to AutoSize so that it resizes according to the image it contains. The Panel will add scrolls to the PictureBox if the image is larger than the display area of the PictureBox.

Making the images resize with the form

When a child window is resized the PictureBox has to be resized to fit in the child window. This can be attained using the following code in the resize event:
private void Form2_Resize(object sender, System.EventArgs e) 
{ 
    //Resize the panel to fit in the form 
    //when the form is maximised or minimised 
    panel1.Width= this.Width-20; 
    panel1.Height=this.Height-40; 
}

Adding a simple text watermark to an image

To add a text watermark to the image in the child window this is what we do:
if (pictureBox1.Image != null)
{
    //Create image.
    Image tmp = pictureBox1.Image;
    //Create graphics object for alteration.
    Graphics g = Graphics.FromImage(tmp);

    //Create string to draw. 
    String wmString = "Code Project"; 
    //Create font and brush.
    Font wmFont = new Font("Trebuchet MS", 10); 
    SolidBrush wmBrush = new SolidBrush(Color.Black); 
    //Create point for upper-left corner of drawing. 
    PointF wmPoint = new PointF(10.0F, 10.0F);
    //Draw string to image.
    g.DrawString(wmString, wmFont, wmBrush, wmPoint);
    //Load the new image to picturebox 
    pictureBox1.Image= tmp;        
    //Release graphics object. 
    g.Dispose();                
}
We create a Graphics object of the image in the PictureBox. The DrawString method of the Graphics object is used to add a watermark text to the image. This method draws a specified text string at the specified location.

Points of Interest

  • When an MDI parent form is closed all child forms are closed before it.
  • Use ActiveMdiChild property of the parent form to find the currently active child window. I have used this to implement the Close child form feature.
  • Setting the MDIList property of a MenuItem to true, will give at runtime, a list of all open child forms. It will display a tick mark next to the active child window. [This is how the Window MenuItem in the sample is implemented].

Acknowledgement



Description :
The following source code will provide the use of MDI Forms in C#.


Source Code:
using
System;using System.Windows.Forms;using System.ComponentModel;using System.Drawing;class MDIDemo:Form
{
private static int
FormCount=0;
MainMenu mnuMain=
new
MainMenu();
MenuItem FileMenu;
MenuItem NewMenu;
MenuItem ExitMenu;
MenuItem WindowMenu;
public
MDIDemo()
{
this.IsMdiContainer=true
;this.Text="MDI Demo";
FileMenu=
new
MenuItem();
FileMenu.Text="&File";
WindowMenu=
new
MenuItem();
WindowMenu.Text="&Window";
WindowMenu.MenuItems.Add("&Cascade",
new
EventHandler(Cascade_Click));WindowMenu.MenuItems.Add("Tile &Horizontally",new EventHandler(TileH_Click));
WindowMenu.MenuItems.Add("Tile &Vertically",
new
EventHandler(TileV_Click));
WindowMenu.MdiList=
true
;
NewMenu=
new
MenuItem();
NewMenu.Text="&New";
NewMenu.Click+=
new
EventHandler(New_Click);
ExitMenu=
new
MenuItem();
ExitMenu.Text="E&xit";
ExitMenu.Click+=
new
EventHandler(Exit_Click);
FileMenu.MenuItems.Add(NewMenu);
FileMenu.MenuItems.Add(
new
MenuItem("-"));
FileMenu.MenuItems.Add(ExitMenu);
mnuMain.MenuItems.Add(FileMenu);
mnuMain.MenuItems.Add(WindowMenu);
this
.Menu=mnuMain;
}
private void Cascade_Click(object
sender,EventArgs e)
{
this
.LayoutMdi(MdiLayout.Cascade);
}
private void TileH_Click(object
sender,EventArgs e)
{
this
.LayoutMdi(MdiLayout.TileHorizontal);
}
private void TileV_Click(object
sender,EventArgs e)
{
this
.LayoutMdi(MdiLayout.TileVertical);}private void New_Click(object sender,EventArgs e)
{
Form frmTemp=
new
Form();
frmTemp.MdiParent=
this
;
frmTemp.Text="Window#" + FormCount.ToString();
FormCount++;
frmTemp.Show();
}
private void Exit_Click(object
sender,EventArgs e)
{
this
.Dispose();
Application.Exit();
}
public static void
Main()
{
Application.Run(
new
MDIDemo());
}
}

Comments