Tek Eye Logo

Tek Eye

MDI Form in C Sharp

This is a brief introduction to a Multiple Document Interface (MDI) example in C#. Don't know about MDI, then see the Tek Eye article What is MDI Form? The MDI User Interface. A quick summary is that MDI is both a style of User Interface (UI), and the name given to the type of window component in an application (the MDI parent form or MDI child form). The MDI UI design style is no longer popular. However, for complex business, industry and science software (especially those running on large displays) it is a useful design pattern.

Multiple Document Interface

Two types of major Microsoft UI technologies can be used in C# for Windows desktop applications, WinForms (short for Windows Forms), and Windows Presentation Foundation (WPF). Both are well established though WinForms is the earlier technology. This tutorial is not concerned with WinForms vs WPF or the relative merits of one over the other. The C# MDI example discussed here uses WinForms because MDI functionality is built in, without the need to use additonal components. Plus the code is straightforward and easy to understand. This article concentrates on the practicallity of WinForms over the beauty of WPF. (This tutorial assumes that Microsoft Visual Studio is installed and a simple C# WinForms project can be created. In which case a MDI program can be developed.)

A C# MDI Project in Visual Studio

An example text editing MDI application is used to show MDI in action. This text editor for Windows shows the classic MDI features. It supports multiple documents and the text editing Windows can minimised, maximised, and dragged around within a larger parent window. Start a new Visual C# Windows Forms Application, here it is called Many Notes. With the Many Notes project highlighted in Visual Studio use the File menu or context menu (normally right-click) and use the Add option. Selecting MDI Parent Form add a new Windows Form named MDIParent.cs.

Visual Studio MDI Parenty

Open the Progam.cs file and change Form1 to MDIParent (or the name given to the MDI parent form). I.e. Application.Run(new Form1()); is changed to Application.Run(new MDIParent());. The project now has a basic MDI skeleton in place with a menu strip, toolbar and status strip. Run the application (use Start). Each click on the document icon (New) will generate a new child form. These forms can be rearranged, maximised, and minimised within the MDI parent. The Windows menu provides options to rearrange all the windows (cascade, tile vertically, tile horizontally).

Adding Simple Text Editing to Each Form

Open MDIParent and change the title, setting the Text property, e.g. to Many Notes. Notice that the IsMdiContainer property is set to true. Now rename Form1.cs to FormNote and select Yes to the question asking to rename all references. Open FormNote and set the Text property (form title) to New Note.

Drop a TextBox on to FormNote and set the Name to TextNote. Set the text box Multiline property to true. Drag the text box to fill the form and set the Anchor to all four sides (Top, Bottom, Left, Right).

In the MDIParent.cs file change the ShowNewForm function to create new instances of FormNote, i.e. change Form childForm = new Form(); to Form childForm = new FormNote();. When the application is now started all the new windows have their own text editing ability. (Note: The TextBox control is very limited for text editing. For a proper note taking application a custom text editor control is more suitable, for example the TFEdit control.)

Uniquely Identify Each Child Form

To help easily identify each child form when the application is running it can be given a unique identifier. Add a public property to the form using public long Id { get; set; }. Now if a specific child form needs special processing it can be found by iterating through all the child forms in the MDI parent code. This is useful when the application is closed and additional checks need to be made for each form (such as saving data).

private void DoFormWork(long FormId)
{
    foreach(Form form in this.MdiChildren)
    {
        if(((FormNote)form).Id == FormId) {
            //do specific form processing
        }
    }
}

Remembering a File Name

As for defining the Id property, a file name can be stored for each child form:

using System.Windows.Forms;

namespace ManyNotes
{
    public partial class FormNote : Form
    {
        public long Id { get; set; }
        public string FileName { get; set; } = string.Empty;

        public FormNote()
        {
            InitializeComponent();
        }
    }
}

See Also

Author:  Published:  

ShareSubmit to TwitterSubmit to FacebookSubmit to Google+Submit to LinkedInSubmit to redditPrint Page

markdown CMS Small Logo Icon ↓markdown↓ CMS is fast and simple. Build websites quickly and publish easily. For beginner to expert.



Articles on:

Android, HTML, VPS, Computing, IT, Computer History, ↓markdown↓ CMS



Free Android Projects and Samples:

Android Examples, Android List Examples, Android UI Examples



Maintain your PC with Piriform:

CCleaner Professional
CCleaner for Mac
Recuva Professional
Defraggler Professional
Speccy Professional

Shop at Tek Eye for PC utilities