Click here to Skip to main content
15,879,326 members
Articles / Web Development / ASP.NET
Article

How to create MDI Application in Compact framework

Rate me:
Please Sign up or sign in to vote.
3.09/5 (8 votes)
15 Apr 20063 min read 49.8K   673   19   8
It provides easy solution dealing multiple forms in .Net Compact framework

Introduction

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!!!

Solution

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:

C#
// 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);
        }
    
        [MTAThread]
        static void Main()
        {
            MotherClass mc = new MotherClass();
            Application.Run(mc.mScreen);
        }
    }
}

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.

C#
private void button1_Click(object sender, EventArgs e)
{
    if (mc.f1 == null)
    {
        mc.f1 = new Form1();
    }
    mc.f1.Show();
}

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.

C#
private void Form1_Closing(object sender, CancelEventArgs e)
{
    e.Cancel = true;
    mc.mScreen.Show();
}

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.

mc.f1.Show();

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.

Advantages

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

mc.f2.show();

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

Conclusion

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.

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


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

Comments and Discussions

 
GeneralExcellent sample!!! Pin
milldoggy18-Apr-08 4:20
milldoggy18-Apr-08 4:20 

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.