MDI (Multiple Document Interface) is nothing but a way of displaying windows form where there is at least one parent and many child windows e.g. 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.
Goto File->New->Blank Solution
Select Visual C# Projects in Project Types
Select WindowsApplication in Templates
Type for eg. myBestMDI in the Name textbox
By Clicking the browse button select the location & Click OK
Creating the necessary forms & code them
Creating the main MDI form (frmMDIMain) to act as a container
- In the View->Solution Explorer click Form1 only once
- Locate the property FileName and type "frmMDIMain.cs"
- In the Solution Explorer now doubleclick frmMDIMain
- Locate the property (Name) and type "frmMDIMain"
- Locate the property Text and type "This is the Parent"
- Locate the property IsMDIContainer and set it to true
- Locate the property WindowState and set it to Maximized
Creating the child form (frmMChild) to demonstrate multiple instance
- Goto Project Menu-Add Windows Forms & type frmMChild
- In the Solution Explorer now doubleclick frmMChild
- Locate the property Text and type "This is the Multi Instance Child"
- Locate the property size & set it to 568, 464
Creating the child form (frmSChild) to demonstrate Single instance
- Goto Project Menu-Add Windows Forms & type frmSChild
- In the Solution Explorer now doubleclick frmSChild
- Locate the property Text and type "This is the Single Instance Child"
- Locate the property size & set it to 568, 464
Creating the About form (frmAbout) to give information about this product
- Goto Project Menu-Add Windows Forms & type frmAbout
- In the Solution Explorer now doubleclick frmAbout
- Locate the property Text and type "About My Best MDI"
- Locate the property size & set it to 448, 304
- Locate the property FormBorderstyle & set it to FixedToolWindow
Creating the Splash form (frmSplash) to display the welcome information with company logo
- Goto Project Menu-Add Windows Forms & type frmSplash
- In the Solution Explorer now doubleclick frmSplash
- Locate the property size and set it to 448, 320
- Locate the property ControlBox and set it to False
- Locate the property FormBoderStyle and set it to None
- Locate the property StartupPosition and set it to CenterScreen
- Locate the property BackGroundImage and browse a nice image along with your company logo
- 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.
- Goto View->Toolbox and Double click Timer control
- Locate timer1 control & click it once
- Set its Interval property to 2000 i.e. in milliseconds
- Set its Enabled property to true
- 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)
- In the Solution explorer right click the Project Name myBestMdi
- Click Add->New Folder and type classes
- right click the Folder classes
- 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
- change the modifiers for the class from public class clsGlobal to public sealed class clsGlobal (This is to protect an instantiation of this class)
- 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
- Again in the Solution explorer right click the Folder classes
- Click Add->Add New Item and class and type clsMain in name
- 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
- 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;
-
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
- Click View->Designer of frmMdiMain
- Goto Toolbox and add a double click MainMenu control
- Click once on the typehere menu
- Locate the property Name & set it to mnuFile
- Locate the property Text & set it to &File
- Note:- & is for underscore which will enable the Hot key 'F'
- Same way click on the sub menu TypeHere box
- Locate the property Name & set it to mnuFileNewMultiple
- Locate the property Text & set it to &New Multiple
- Locate the property ShortCut & choose CtrlN
- Same way click on the sub menu TypeHere box
- Locate the property Name & set it to mnuFileNewSingle
- Locate the property Text & set it to New & Single
- click on the sub menu TypeHere box
- Locate the property Name & set it to mnuFileCloseChild
- Locate the property Text & set it to &Close Child
- click on the sub menu TypeHere box
- Locate the property Name & set it to mnuFileSep1
Locate the property Text & set it to -
Note - is a hyphen which will automatically put a whole seperator
- click on the sub menu TypeHere box
- Locate the property Name & set it to mnuFileExit
- Locate the property Text & set it to Exit
Now I am sure by now you should be able to create one more menu
- 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
- 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
- Now in the frmSChild code add the following lines
private static frmSChild m_SChildform;
//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 tpublic 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
}
- Click View->Designer of frmMdiMain again
- Double click New Single Menu on MDI main form
add the following code
frmSChild objfrmSChild = frmSChild.GetChildInstance();
objfrmSChild.MdiParent = this;
objfrmSChild.Show();
objfrmSChild.BringToFront();
- Click View->Designer of frmMdiMain again
- Double click New Multi Menu on MDI main form add the following code
frmMChild objfrmMChild = new frmMChild();
objfrmMChild.MdiParent = this;
objfrmMChild.Show();
- 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
{
}
- Double click Exit on MDI main form add the following code
Application.Exit();
- Double click Cascade menu under Windows on MDI main form add the following code
this.LayoutMdi(MdiLayout.Cascade);
- same way for tile Vertical add
this.LayoutMdi(MdiLayout.TileVertical);
- and for tile horizontal add
this.LayoutMdi(MdiLayout.TileHorizontal);
- Now finally double click About menu in Help to add
frmAbout objfrmAbout = new frmAbout();
objfrmAbout.ShowDialog();
- Press F5 to start with debugging or Ctrl+F5 to start w/o debugging
Njoi programming!!