Click here to Skip to main content
15,391,989 members
Articles / Programming Languages / C#
Posted 24 Jan 2003


280 bookmarked

Introduction to MDI Forms with C#

Rate me:
Please Sign up or sign in to vote.
4.77/5 (212 votes)
24 Jan 2003CPOL4 min read
An article on how to develop an MDI application using C# and the .NET Framework.


This article tries to explain how to develop an MDI application. Text editors and image editors are common examples where multiple document interfaces 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 

//Display the child window

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:

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.

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.        

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) 

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; 

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. 

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].


  • C# Guru - James T Johnson, for enlightening me on how Panel Class can be effectively used to avoid scrollbar headache.
  • Nish, for reviewing and commenting on this work.
  • All CP'ians who encouraged my first article, prompting me to write another, when I myself had to do some learning on MDI forms.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Smitha Nishant
The Code Project
United States United States
Smitha is a software developer, and has been in the industry since January 2000. She has experience in ASP.NET, C#, Windows Forms, Visual Basic, ASP, JavaScript, VBScript, and HTML. She has been with CodeProject since 2003 and currently works as Senior Editor.

In her free time, she tries out new dishes, reads a little, and puts together jigsaw puzzles. Originally from Trivandrum, Smitha currently lives with her husband and fellow CP'ian Nish [^] and her son Rohan in Columbus, Ohio.

Comments and Discussions

Questionretrieving child form data to parent form Pin
Member 1388121420-Jun-18 23:40
MemberMember 1388121420-Jun-18 23:40 
PraiseAmazing! Pin
tuxmc22-Nov-17 12:53
Membertuxmc22-Nov-17 12:53 
QuestionThank you Pin
star start 20-Oct-16 17:44
Memberstar start 20-Oct-16 17:44 
QuestionHow to use this technique in SplitContainer Pin
ErimZ10-Jun-16 4:16
MemberErimZ10-Jun-16 4:16 
QuestionMDI form child Maximize behavior Pin
talat karim6-May-16 2:10
Membertalat karim6-May-16 2:10 
QuestionThanks for this very instructive article Pin
guntbert10-May-15 7:37
Memberguntbert10-May-15 7:37 
GeneralMy vote of 4 Pin
Santhakumar Munuswamy @ Chennai3-May-15 5:04
professionalSanthakumar Munuswamy @ Chennai3-May-15 5:04 
GeneralNice Example Pin
Member 1103070626-Jan-15 5:12
MemberMember 1103070626-Jan-15 5:12 
GeneralThanks Pin
Сергей Чеботарев10-Nov-14 0:39
MemberСергей Чеботарев10-Nov-14 0:39 
GeneralMy vote of 2 Pin
Member 105072406-Jan-14 13:20
MemberMember 105072406-Jan-14 13:20 
AnswerRe: My vote of 2 Pin
Leo Tully17-Mar-14 9:48
MemberLeo Tully17-Mar-14 9:48 
OpenXLive24-Apr-13 22:32
MemberOpenXLive24-Apr-13 22:32 
GeneralMy vote of 4 Pin
upendra kumar pandey23-Jan-13 23:03
Memberupendra kumar pandey23-Jan-13 23:03 
QuestionMDI Forms Pin
Chris@noruo3-Oct-12 5:36
MemberChris@noruo3-Oct-12 5:36 
GeneralMy vote of 5 Pin
Member 93353509-Sep-12 3:41
MemberMember 93353509-Sep-12 3:41 
Questionmdi parent form Pin
arun_kamboj15-Aug-12 11:50
Memberarun_kamboj15-Aug-12 11:50 
Questionhow to hide main panel of child form in which the minimize and maximize button is situated Pin
Aditya Singh23-Jul-12 21:32
MemberAditya Singh23-Jul-12 21:32 
QuestionMDI Form in C# 3.5 Pin
Dibu.net15-Jul-12 5:52
professionalDibu.net15-Jul-12 5:52 
Questionc# to solve ant colony Pin
Member 80430366-May-12 4:44
MemberMember 80430366-May-12 4:44 
GeneralMy vote of 5 Pin
Shahin Khorshidnia12-Mar-12 10:41
professionalShahin Khorshidnia12-Mar-12 10:41 
GeneralMy vote of 5 Pin
Member 249996118-Feb-12 23:58
MemberMember 249996118-Feb-12 23:58 
GeneralMy vote of 5 Pin
version_2.016-Aug-11 21:14
Memberversion_2.016-Aug-11 21:14 
QuestionMy vote of 5 Pin
Massimo Conti21-Jun-11 10:29
MemberMassimo Conti21-Jun-11 10:29 
GeneralMy vote of 5 Pin
mc_avr9-May-11 19:47
Membermc_avr9-May-11 19:47 
GeneralMy vote of 5 Pin
darshan yadav23-Dec-10 20:17
Memberdarshan yadav23-Dec-10 20:17 

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.