Developing MDI Applications in C#

Introduction

In this article, I will explain basics of Multiple Document Interface (MDI) applications. MDI applications let you to show multiple documents at the same time, with every document displayed in its individual window.

Creating MDI Parent Forms

The base of a Multiple Document Interface (MDI) is the MDI parent form. This is the form that holds the MDI child windows, which are all the "sub-windows" in which the client work together with the MDI application.

To Create MDI Parent Forms:

Create a new form and add the code.

this.IsMDIContainer = true;

This assign the form as an MDI container for child windows.

Creating MDI Child Forms

An vital constituent of Multiple Document Interface (MDI) Applications is the MDI child forms, as these are the main windows for client interaction.

To Create MDI Child Forms:

Form frmchild=new Form();
frmchild.MDIParent=
this;
frmchild.Show();

Determining the Active MDI Child:

In a number of circumstance, we desire to give a command that operates on the control with the focus on the at present active child form.

For the reason that an application can have many instances of the same child form, the process wants to be acquainted with which form to use. To specify this, use the ActiveForm property of an MDI Form, which returns the child form that has the focus.

In any case one MDI child form must be loaded and visible when you access the ActiveForm property, or an error is returned.

Arranging Child Forms:

Frequently, applications will have menu commands for actions such as Tile, Cascade, and Arrange, with concerning to the open MDI child forms. One can use the LayoutMDI method with the MDILayout enumeration to rearrange the child forms in an MDI parent form.

The MDILayout enumeration can be set to four different values, which will display child forms as cascading, as horizontally or vertically. Often, these methods are used as the event handlers called by a menu item's Click event. In this way, a menu item with the text "Cascade Windows" can have the desired effect on the MDI child windows.

In the example below, the event handler for the Click event for the Cascade menu item sets the MDILayout enumeration to Cascade for the child windows of the MDI Parent form.

Example:

using System;
using System.ComponentModel;
using System.WinForms;
using System.Drawing;
public class MDI :Form
{
private MainMenu mainMenu;
private int Count=0;
public MDI()
{
this.IsMDIContainer=true;
this.Text="MDI Demo";
mainMenu =
new MainMenu();
MenuItem File = mainMenu.MenuItems.Add("&File");
File.MenuItems.Add(
new MenuItem("&New",new EventHandler this.FileNew_clicked),Shortcut.CtrlN));
File.MenuItems.Add(
new MenuItem("&Active Child",new EventHandler this.FindActive_clicked),Shortcut.CtrlA));
File.MenuItems.Add(
new MenuItem("-"));
File.MenuItems.Add(
new MenuItem("&Exit",new EventHandler this.FileExit_clicked),Shortcut.CtrlX));
MenuItem Arrange = mainMenu.MenuItems.Add("&Arrange");
Arrange.MenuItems.Add(
new MenuItem("&Cascade",new EventHandler this.Cascade_clicked),Shortcut.F1));
Arrange.MenuItems.Add(
new MenuItem("&Horizontal",new EventHandler this.Horizontal_clicked),Shortcut.F2));
Arrange.MenuItems.Add(
new MenuItem("&Vertical",new EventHandler this.Vertical_clicked),Shortcut.F3));
this.Menu=mainMenu;
mainMenu.GetForm().BackColor = Color.Indigo ;
}
private void FileExit_clicked(object sender, EventArgs e)
{
this.Close();
}
private void FindActive_clicked(object sender, EventArgs e)
{
MessageBox.Show(
this.ActiveMDIChild.Text,"MDI FORM",MessageBox.IconInformation);
}
private void FileNew_clicked(object sender, EventArgs e)
{
Form frmchild=
new Form();
frmchild.MDIParent=
this;
frmchild.Show();
frmchild.Text="Child Form" + Count.ToString();
Count++;
}
private void pop_Clicked(object sender, EventArgs e)
{
MessageBox.Show("Popupmenu","MENU_CREATION",MessageBox.IconInformation);
}
private void Cascade_clicked(object sender, EventArgs e)
{
this.LayoutMDI(MDILayout.Cascade );
}
private void Horizontal_clicked(object sender, EventArgs e)
{
this.LayoutMDI(MDILayout.TileHorizontal);
}
private void Vertical_clicked(object sender, EventArgs e)
{
this.LayoutMDI(MDILayout.TileVertical);
}
public static void Main(string[] args)
{
Application.Run(
new MDI());
}
}

Output:

MdiAppGAG1.jpg

MDICascadeGAG2.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all