Click here to Skip to main content
11,641,081 members (60,527 online)
Click here to Skip to main content
Articles » Languages » C# » General » Revisions

Creating MDI application using C# (Walkthrough)

, 29 Jun 2004 229.8K 6.9K 49
Rate this:
Please Sign up or sign in to vote.
If you are a beginner(or intermediate) and want to develop an MDI application in C# then this one is for you!!
This is an old version of the currently published article.
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS--><!-- Download Links -->
  • <A href="myBestMDI/myBestMDI_src.zip"></A><A href="myBestMDI/myBestMDI_src.zip"></A><A href="myBestMDI/myBestMDI_src.zip">Download source files - 71.2 Kb</A> 
<!-- Article image -->

Sample Image - myBestMDI.jpgDownload sourcecode

<!-- Add the rest of your HTML here -->

Introduction


MDI (Multiple Document Interface) is nothing but a way of displaying windows form where there is atleast one parent and many
child windows eg. word Excel Powerpoint kind of windows where each document , sheet or slide act as a child under the parent
container window.
SDI(Single document Interface) examples are Windows Explorer,Internet explorer,Notepad etc...where only one window acts
as an interface.

If you are a beginner(or intermediate) and want to develop an MDI application with basic functionality using the powerful
C# language checkout the following step-by-step guide to develop. Even people coming from VB6 background face lots of
problem because of the pure OOPs usage in C#. Follow this small step by step walkthrough to make a small MDI application.


STEP BY STEP WALKTHROUGH:-

1> Goto File->New->Blank Solution
2> Select Visual C# Projects in Project Types
3> Select WindowsApplication in Templates
4> Type for eg. myBestMDI in the Name textbox
5> By Clicking the browse button select the location & Click OK


CREATING ALL THE NECESSARY FORMS & CODE THEM
============================================

Creating the main MDI form(frmMDIMain) to act as a container
------------------------------------------------------------

1> In the View->Solution Explorer click Form1 only once
2> Locate the property FileName and type "frmMDIMain.cs"
3> In the Solution Explorer now doubleclick frmMDIMain
4> Locate the property (Name) and type "frmMDIMain"
5> Locate the property Text and type "This is the Parent"
6> Locate the property IsMDIContainer and set it to true
7> Locate the property WindowState and set it to Maximized

Creating the child form (frmMChild) to demonstrate multiple instance
--------------------------------------------------------------------

1> Goto Project Menu-Add Windows Forms & type frmMChild
2> In the Solution Explorer now doubleclick frmMChild
3> Locate the property Text and type "This is the Multi Instance Child"
4> Locate the property size & set it to 568, 464

Creating the child form (frmSChild) to demonstrate Single instance
--------------------------------------------------------------------

1> Goto Project Menu-Add Windows Forms & type frmSChild
2> In the Solution Explorer now doubleclick frmSChild
3> Locate the property Text and type "This is the Single Instance Child"
4> Locate the property size & set it to 568, 464

Creating the About form (frmAbout) to give information about this product
-------------------------------------------------------------------------

1> Goto Project Menu-Add Windows Forms & type frmAbout
2> In the Solution Explorer now doubleclick frmAbout
3> Locate the property Text and type "About My Best MDI"
4> Locate the property size & set it to 448, 304
5> Locate the property FormBorderstyle & set it to FixedToolWindow

Creating the Splash form (frmSplash) to display the welcome information with company logo
---------------------------------------------------------------------------------------
1> Goto Project Menu-Add Windows Forms & type frmSplash
2> In the Solution Explorer now doubleclick frmSplash
3> Locate the property size and set it to 448, 320
4> Locate the property ControlBox and set it to False
5> Locate the property FormBoderStyle and set it to None
6> Locate the property StartupPosition and set it to CenterScreen
7> Locate the property BackGroundImage and browse a nice image along with your company logo
8> Locate the property cursor & set it to waitCursor

Adding a timer control to the splash screen for holding it for few seconds so
that the user can read it.
---------------------------------------------------------------------------------------------

1> Goto View->Toolbox and Double click Timer control
2> Locate timer1 control & click it once
3> Set its Interval property to 2000 i.e. in milliseconds
4> Set its Enabled property to true
5> Double click to reach its Tick event
type :-
timer1.Enabled = false;
this.Close();
Adding two classes 1 to act as the Main startup class(clsMain) & 1 for holding
Global objects(clsGlobal)
---------------------------------------------------------------------------------------------

1> In the Solution explorer right click the Project Name myBestMdi
2> Click Add->New Folder and type classes
3> right click the Folder classes
4> Click Add->Add New Item and class and type clsGlobal in name
Note:- myBestMDI.Classes is automatically taken as the namespace due to folder level
5> change the modifiers for the class from

 public class clsGlobal 

to
 public sealed class clsGlobal 

(This is to protect an instantiation of this class)

 

6> Just after the curly braces of the class starts paste the following line
public static frmMDIMain g_objfrmMDIMain;
//this is to declare a global static reference to our MDI parent so that the same
//instance is referred across all child with no extra line of code

7> Again in the Solution explorer right click the Folder classes
8> Click Add->Add New Item and class and type clsMain in name
9> just below the constructor clsMain function in the clsMain class paste the following
snippet

[STAThread]
  static void Main() 
  {
   try
   {
    frmSplash objfrmSplash = new frmSplash();
    objfrmSplash.ShowDialog();
    clsGlobal.g_objfrmMDIMain = new frmMDIMain();
    Application.Run(clsGlobal.g_objfrmMDIMain);
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message,"My Best MDI",MessageBoxButtons.OK,MessageBoxIcon.Stop);            
   }
  }
//This is the Single Threaded Apartment Model(out of our scope) of the application which will run the Main function
//when the application starts


10> Since we are using Application class and MessageBox functions in the above code we need a
add the foolowing line on the top
using System.Windows.Forms;
11> Now go to frmMain now and from its code window remove the following piece of code

[STAThread]
  static void Main() 
  {
   Application.Run(new Form1());
  }
//because we cannot have two Main function.We are invoking everything from clsMain

Creating menus in the main MDI form
--------------------------------------

1> Click View->Designer of frmMdiMain
2> Goto Toolbox and add a double click MainMenu control
3> Click once on the typehere menu
4> Locate the property Name & set it to mnuFile
5> Locate the property Text & set it to &File
Note:- & is for underscore which will enable the Hot key 'F'
6> Same way click on the sub menu TypeHere box
7> Locate the property Name & set it to mnuFileNewMultiple
8> Locate the property Text & set it to &New Multiple
9> Locate the property ShortCut & choose CtrlN
10> Same way click on the sub menu TypeHere box
11> Locate the property Name & set it to mnuFileNewSingle
12> Locate the property Text & set it to New &Single
13> click on the sub menu TypeHere box
14> Locate the property Name & set it to mnuFileCloseChild
15> Locate the property Text & set it to &Close Child
16> click on the sub menu TypeHere box
17> Locate the property Name & set it to mnuFileSep1
18> Locate the property Text & set it to - 
Note - is a hyphen which will automatically put a whole seperator
19> click on the sub menu TypeHere box
20> Locate the property Name & set it to mnuFileExit
21> Locate the property Text & set it to E&xit

Now i am sure by now you should be able to create one more menu
22> Create one more top level menu with Name mnuWindow and text &Window
set its MDIList property to true(will show names of opened windows)
with 3 sub menus with name
mnuWindowCascade and text &Cascade
mnuWindowTileVertical and text Tile&Vertical
mnuWindowTileHorizontal and text Tile&Horizontal

23> Create one more top level menu with Name mnuHelp and text &Help
with a sub menu with name mnuHelpAbout and text &About...
'...' as the suffix is just a Microsoft convention to signify that the command will show a dialog box.

Putting life in the menus created by adding code to it
--------------------------------------------------------

1> Now in the frmSChild code add the following lines

  private static frmSChild m_SChildform;
  public static frmSChild GetChildInstance()
  {
   if (m_SChildform ==null) //if not created yet, Create an instance
    m_SChildform = new frmSChild();
   return m_SChildform;  //just created or created earlier.Return it
  }

//This is to make sure that when we Click on a 'New Single' menu on Parent form twice 
//it should not open two instance of the same child form

2> Click View->Designer of frmMdiMain again
3> Double click New Single Menu on MDI main form
add the following code

frmSChild objfrmSChild = frmSChild.GetChildInstance();
objfrmSChild.MdiParent = this;
objfrmSChild.Show();
objfrmSChild.BringToFront();



4> Click View->Designer of frmMdiMain again
5> Double click New Multi Menu on MDI main form
add the following code

frmMChild objfrmMChild = new frmMChild();
objfrmMChild.MdiParent = this;
objfrmMChild.Show();

6> Double click CloseChild on MDI main form
add the following code

try
{
 if(this.ActiveMdiChild.Name =="frmMChild")
 {
  frmMChild objfrmMChild = (frmMChild)this.ActiveMdiChild;
  objfrmMChild.Close();
 }
 else
 {
  frmSChild objfrmSChild = (frmSChild)this.ActiveMdiChild;
  objfrmSChild.Close();

 }
}
catch
{
}

7> Double click Exit on MDI main form
add the following code

Application.Exit(); 


8> Double click Cascade menu under Windows on MDI main form
add the following code

this.LayoutMdi(MdiLayout.Cascade);


9> same way for tile Vertical add

this.LayoutMdi(MdiLayout.TileVertical);


81> and for tile horizontal add

this.LayoutMdi(MdiLayout.TileHorizontal);

20> Now finally double click About menu in Help to add

frmAbout objfrmAbout = new frmAbout();
objfrmAbout.ShowDialog();

21> Press F5 to start with debugging or Ctrl+F5 to start w/o debugging

 Sample screenshot

Njoi programming!!


 

 

 

 

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

Share

About the Author

irfan patel
Web Developer
United Kingdom United Kingdom
Irfan Patel is a Bachelor of Computer Application
He holds a Microsoft Certified Solution Developer Certificate and has worked extensively with COM and has been programming since 1998.
He has also trained more than 200 students and has taught various languages,RDBMS & platforms
from C,C++ to VB with Oracle,Access & MS SQLServer
from Vbscript,Javascript to ASP.
His expertise comes from various industries viz Jewellery, Shipping ,Automobile ,
Radio Frequency Devices, Photometers to name few.
Besides programming he loves music,singing,dancing, bike riding & cricket.

You may also be interested in...

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
Questionthanks Pin
Member 899021615-Dec-13 2:56
memberMember 899021615-Dec-13 2:56 
GeneralMy vote of 5 Pin
George Dawes4-Feb-13 0:17
memberGeorge Dawes4-Feb-13 0:17 
GeneralMy vote of 3 Pin
Ali Agha25-Jan-13 12:23
memberAli Agha25-Jan-13 12:23 
GeneralGreat tutorial - Here are some bug fixes Pin
Member 947983431-Dec-12 7:21
memberMember 947983431-Dec-12 7:21 
GeneralRe: Great tutorial - Here are some bug fixes Pin
Ciorbea21-Aug-13 4:54
memberCiorbea21-Aug-13 4:54 
GeneralMy vote of 3 Pin
Kshitiz Chandra17-Aug-12 17:30
memberKshitiz Chandra17-Aug-12 17:30 
GeneralMy vote of 5 Pin
battzion14-Aug-12 21:15
memberbattzion14-Aug-12 21:15 
GeneralMy vote of 5 Pin
Member 432084410-Feb-12 5:33
memberMember 432084410-Feb-12 5:33 
QuestionAdaptations in newer versions of Visual C# Pin
harryjansens19-Jun-11 5:01
memberharryjansens19-Jun-11 5:01 
Questionwhen i tried this codes.... an error " Cannot access a disposed object. Object name: 'frmSChild...:" this happens when i select "new single" from "File" menu Second time.. Pin
Ragigopi3-Jun-11 21:19
memberRagigopi3-Jun-11 21:19 
GeneralMy vote of 5 Pin
elTorito6667-Apr-11 5:01
memberelTorito6667-Apr-11 5:01 
Generalcannot set startup object to "Main" class Pin
vegeta4ss15-Jan-10 13:10
membervegeta4ss15-Jan-10 13:10 
Questionto get the mdichild form's richtextbox contents and store it in mdiparent Pin
brindhakr9-May-09 2:11
memberbrindhakr9-May-09 2:11 
Generalnever mind I found the AllowMerge property Pin
ThorTheBraveGod14-Apr-09 9:59
memberThorTheBraveGod14-Apr-09 9:59 
GeneralMenu in the multi forms Pin
ThorTheBraveGod14-Apr-09 9:36
memberThorTheBraveGod14-Apr-09 9:36 
Generalthanx Pin
kanza azhar29-Nov-08 9:50
memberkanza azhar29-Nov-08 9:50 
GeneralERROR when closing single child and re-open it Pin
TheCubanP18-Dec-06 10:51
memberTheCubanP18-Dec-06 10:51 
GeneralRe: ERROR when closing single child and re-open it Pin
Martyn J Jones14-Jun-07 1:41
memberMartyn J Jones14-Jun-07 1:41 
QuestionNaming Conventions and Standards? Pin
altair202021-Sep-06 12:56
memberaltair202021-Sep-06 12:56 
GeneralError Pin
airfatter27-Jun-06 23:33
memberairfatter27-Jun-06 23:33 
GeneralRe: Error Pin
Ent194-Oct-06 2:58
memberEnt194-Oct-06 2:58 
Questionclose all child window Pin
parivartan12-Jan-06 22:30
memberparivartan12-Jan-06 22:30 
AnswerRe: close all child window Pin
dapoussin4-Apr-06 3:36
memberdapoussin4-Apr-06 3:36 
GeneralMultiple child Pin
Anonymous12-May-05 18:13
sussAnonymous12-May-05 18:13 
GeneralRe: Multiple child Pin
legend_rajesh28-Mar-06 23:11
memberlegend_rajesh28-Mar-06 23:11 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 30 Jun 2004
Article Copyright 2004 by irfan patel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid