Click here to Skip to main content
13,800,546 members
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 15 Apr 2006

How to create MDI Application in Compact framework

, 15 Apr 2006
Rate this:
Please Sign up or sign in to vote.
It provides easy solution dealing multiple forms in .Net Compact framework


In .NET CF we are bounded by a lot of limitations mostly of them are memory and speed related.

When I was new to CF, I faced problem while developing an MDI application.

The basic problem while developing multiple document application is that as soon as one closes the form called first in the application, the whole application will be closed. Some how I got a solution which I feel very easy to implement .Just a simple trick!!!


When you want to develop an application I feel that it is better to create a mother class where you can define variables common to all the forms which give the dimension of maintainability to the code. This mother class will have the instances of all the forms and all the forms will have instance of this mother class. This way it will be easy to interlink all the forms with Mother Class as well as child forms themselves.

The following code shows how to create the controlling mother class:

// Example of Mother Class

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace MultipleDocments_Demo
    public class MotherClass
        public Form1 f1;
        public Form2 f2;
        public Form3 f3;
        public mainscreen mScreen;
        public MotherClass()
            mScreen = new mainscreen(this);
        static void Main()
            MotherClass mc = new MotherClass();

So now first of all the main screen will appear which you can compare with the MDI parent form in the Frame work. Here one can provide the options of opening of remaining child forms. In the main form I have provided one of the most optimal solutions for form opening in the following lines of code.

private void button1_Click(object sender, EventArgs e)
    if (mc.f1 == null)
        mc.f1 = new Form1();

In this code when one clicks on button1 for opening form1, it checks whether the form is being opened first time by checking against null. If yes then it creates the instance that time only. So no instance creation for the forms which are never clicked in the application. Instead of creating instance of the form in start (i.e. in mother class) where it can increase the loading time of application, one should create the instance of form only when needed.

After opening a form, providing it all the input, when one closes the form, it disappears in background instead of getting closed .It is due to the following lines of code where I cancels the closing of the form and brings the main screen in front.

private void Form1_Closing(object sender, CancelEventArgs e)
    e.Cancel = true;

Now if the user again clicks the button1 to open the form1, this time the object is already exists , so it brings the form in from without going in the if loop by executing show function of the form.


But remember one thing that as soon as one closes the main screen, the application will exit by itself as this time we have not cancelled the closing event of main screen.


1) In this method the loading time of the application reduces because we are not creating forms intially, it is created only when one need to open that particular form.

2) Once the form is opened , it remains opened .We can play the game of hide and seek till the application ends, just by cancelling the closing of the form and bringing the mainscreen in from at right moment.

3) The mother class provides the opportunity to declare the variables which are used in all the forms or in most of the forms similar to the global variables in C++ which are absent in .Net CF. And just with the instance of Mother class those variables can be used in that particular form.

4) Due to creating the instance of the Mother class to each form , it is easier to call the other forms also in a particular one.For e.g. in form one we can call;

which will bring the form2 in front instead of mainscreen if it already exists.


Implementation of the multiple document interface can be done in a lot of ways but this trick made my application behave in one of the optimized way .The maintainability is also easy.


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


About the Author

I am a .Net CF developer.I am passionate about coding,sketching,blogging, travelling.One can read my blogs @
I wish all the best to CF Developer community for the future challenges.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
scortator19-Sep-09 7:47
memberscortator19-Sep-09 7:47 
GeneralMotherClass as a Singleton Pin
izror14-Sep-09 6:11
memberizror14-Sep-09 6:11 
GeneralRe: MotherClass as a Singleton Pin
Augustine Oh22-Sep-09 5:34
memberAugustine Oh22-Sep-09 5:34 
GeneralA good article but i have a problem Pin
Member 28551358-Oct-08 1:25
memberMember 28551358-Oct-08 1:25 
GeneralPerfect Example Pin
Dimitris Nemtsov19-Sep-08 13:23
memberDimitris Nemtsov19-Sep-08 13:23 
QuestionCan u give an example in Pin
Shekoman6911-Jul-08 15:22
memberShekoman6911-Jul-08 15:22 
GeneralExcellent sample!!! Pin
milldoggy18-Apr-08 5:20
membermilldoggy18-Apr-08 5:20 
NewsSource and demo are not found Pin
musicinc_2615-Apr-06 3:24
membermusicinc_2615-Apr-06 3:24 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.181215.1 | Last Updated 15 Apr 2006
Article Copyright 2006 by ashwani_singh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid