Multiple Document Interface(MDI) Application

Introduction:
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.
MDI Application:
Multiple-document interface (MDI) applications enable you to display multiple documents at the same time, with each document displayed in its own window. MDI applications often have a Window menu item with submenus for switching between windows or documents.
To create an MDI parent form at design time:

MDI fundamentals:

A multiple-document interface, MDI, is an application that primarily has a form and a menu. Some, if not most MDIs also have one or more toolbars and/or a status bar. Here is an example:

 

12

Like a normal application, to use an MDI, the user must launch it. In some cases, when the application starts, it is empty; that is, no document is created and the title bar displays a caption, usually the name of the application.

Usually, there are steps the user must follow to create a document. In some other cases, when the application is launched, it automatically creates a document. A document resides inside the parent frame of the application. That is, a child document can use only the area reserved for it. The child document has its own system icon, its own title bar, and its system buttons (Minimize, Maximize/Restore, and Close).

To use the whole area, the user can maximize the child document. When this is done, the child merges its title bar with the parent’s. The new caption of the title bar becomes made of the text of the parent, followed by -, and followed by the caption the child window was using. The system buttons of the child document display under those of the parent frame:

11

Once a document has been created, the user can use it. Normalement, the application must give the user the ability to create other documents while still using the application. If many documents have been created, all of them are confined in the frame of the application:

10

The user can maximize the child forms. If so, the document that was in front occupies the whole area devoted to child documents. The other child forms stay in the back but become invisible.

Creating an MDI Application:

using System;
using System.Drawing;
using System.Windows.Forms;

public class CommonParent : Form
{
    public CommonParent()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {

    }

    [STAThread]
    public static int Main()
    {
        Application.Run(new CommonParent());

        return 0;
    }
}

The primary form of an MDI application is referred to as the parent or MDI container. It provides the frame inside of which the documents will reside. To provide this functionality, the Form class is equipped with a Boolean property named IsMdiContainer. Therefore, after creating the first form of your application, to indicate that it acts as the main frame, set this property to true. You can do this in the Properties window if you are visually creating your application, or programmatically. Here is an example:

using System;
using System.Drawing;
using System.Windows.Forms;

public class CommonParent : Form
{
    public CommonParent()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        this.IsMdiContainer = true;
    }

    [STAThread]
    public static int Main()
    {
        Application.Run(new CommonParent());

        return 0;
    }
}

This would produce:

1

The primary characteristic of an MDI is that it contains other forms. These forms must be created and made available to the parent. Each form can be created using a predefined form or you can programmatically create one by declaring an object of type Form. To allow you to specify that a form has a parent and will act as a child, the Form class is equipped with a property named MdiParent. This is a read-write property. The set accessor indicates what form acts as this one’s parent. To provide this information, assign the main form this form’s MdiParent. After doing this, you can display the form when you are ready, by calling its Show() method. Here is an example:

using System;
using System.Drawing;
using System.Windows.Forms;

public class CommonParent : Form
{
    public CommonParent()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        this.IsMdiContainer = true;

        Form frmChild = new Form();
        frmChild.MdiParent = this;

        frmChild.Show();
    }

    [STAThread]
    public static int Main()
    {
        Application.Run(new CommonParent());

        return 0;
    }
}

this will result:

9

Creating MDI APP...
  1. Click the form to give it focus
  2. In the Properties window, change the following characteristics:
    IsMdiContainer: True
    StartPosition: CenterScreen
    Text: Notice

N1

  1. To add a new form, on the main menu, click Project -> Add Windows Form…
  2. Set the Name to SingleDocument
  3. Click Add
  4. From the Common Controls section of the Toolbox, click RichTextBox Rich Text Box and click the form
  5. In the Properties window, change the following characteristics:
    (Name): rtbNotice
    Dock: Fill
    Modofiers: Public
  6. On the form, click File and double-click New
  7. Implement the event as follows:
    private void mnuFileNew_Click(object sender, EventArgs e)
    {
        SingleDocument document = new SingleDocument();
                document.Text = "Untitled";
    
        document.MdiParent = this;
        document.Show();
    
        mnuFileClose.Enabled = true;
    }
  8. Return to the form
  9. On its toolbar, click the first button (New)
  10. Click the Events button Events of the Properties window. Click the Click field and, on the right side, select mnuFileNew_Click
  11. On the form, click File and double-click Open
  12. Implement the event as follows:
    private void mnuFileOpen_Click(object sender, EventArgs e)
    {
        if (dlgFileOpen.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            foreach (string strFile in dlgFileOpen.FileNames)
            {
                SingleDocument document = new SingleDocument();
    
                document.rtbNotice.LoadFile(strFile);
    
                document.MdiParent = this;
                document.Show();
            }
        }
    }
  13. Return to the form
  14. On its toolbar, click the second button (Open)
  15. Click the Events button Events of the Properties window. Click the Click field and, on the right side, select mnuFileOpen_Click
  16. Return to the form

MDI Characteristics:

When you create an MDI application, you must make sure you provide your users with the ability to create documents. In fact, probably one of your first assignments is to make sure the user can create as many documents as necessary. As the documents are created, you need a way to programmatically keep track of the child forms. For example, you can store the documents in a collection. To assist you with this, the Form class is equipped with a property named MdiChildren, which is a read-only array. Each element of the MdiChildren[] array is of type Form. Therefore, to access a child form of an MDI applications, you can pass an index to this property.

Child form Layout:

Based on the standards defined in the operating system, as child forms are created in an MDI application, they are positioned each on top of the previous one but below the next one. The arrangement uses a 3-D coordiniate system whose origin is on the lower-left corner of the parent’s frame just under the title bar (or the menu, if any; or the toolbar, if any), with the Z avis moving from the monitor towards you:

2

The operating system allows the user to choose among different arrangements. For example, you can position the documents as vertical columns, as horizontal rows, or as tiles. To support this, the Form class is equipped with a method named LayoutMdi. Its syntax is:

public void LayoutMdi(MdiLayout value);

The LayoutMdi() method takes an argument that is a member of the MdiLayout enumeration. The members of this enumeration are Cascade, TileHorizontal, TileVertical, and ArrangeIcons.

Laying out the Documents:

  1. Make sure the Notice form is displaying.
    Under the form, click mnuMain
  2. Under the Properties window, click Edit Items…
  3. In the Select Item And Add To List Below, make sure MenuItem is selected and click Add
  4. Change its Text to &Window and its (Name) to mnuWindow
  5. Use the up button up to move and position it just above mnuHelp

12

 

  1. In the Select Item And Add To List Below, make sure MenuItem is selected and click Add
  2. Change its Text to &Window and its (Name) to mnuWindow
  3. While mnuWindow is selected, in the right list, click DropDownItems and click its button
  4. Use the same button to create items with the following characteristics:
  5. Text (Name)
    &Arrange mnuWindowArrange
    &Cascade mnuWindowCascade
    Tile &Horizontal mnuWindowTileHorizontal
    Tile &Vertical mnuWindowTileVertical
    1. Implement the event as follows:
      private void mnuWindowArrange_Click(object sender, EventArgs e)
      {
          LayoutMdi(MdiLayout.ArrangeIcons);
      }
    2. Return to the form
    3. Double-click Cascade
    4. Implement the event as follows:
      private void mnuWindowCascade_Click(object sender, EventArgs e)
      {
          LayoutMdi(MdiLayout.Cascade);
      }
    5. Return to the form
    6. Double-click Tile Horizontal
    7. Implement the event as follows:
      private void mnuWindowTileHorizontal_Click(object sender, EventArgs e)
      {
          LayoutMdi(MdiLayout.TileHorizontal);
      }
    8. Return to the form
    9. Double-click Tile Vertical
    10. Implement the event as follows:
      private void mnuWindowTileVertical_Click(object sender, EventArgs e)
      {
          LayoutMdi(MdiLayout.TileVertical);
      }
    11. Return to the form

    Activating the Child:

In most MDI applications, a user can create as many documents as necessary. This also means that the application can hold many child forms. To access a child form, the user can click its title bar. You can also provide options on a menu item named Window that would display a list of open documents.

When a child window is activated, it fires an event named MdiChildActivate:

public event EventHandler MdiChildActivate;

The MdiChildActivate event is of type EventArgs.

The document that is currently active as a brighter title bar. To document this document, the form has a property named ActiveMdiChild. This read-only property allows you to know what document is the current active one. This property is of type Form, which means its produces a Form object. When you enquire about this property, if its value is null, it means there is no current active document.

If the value of theActiveMdiChild property is not null, a document is active and you can use it. If you want to access the objects (Windows controls) that are positioned on a child form, remember that the child form is somehow foreign. Therefore, you should set the Modifiers property of its hosted controls approppriately. For example, if you want the parent frame to access a control on the child frame, set the Modifiers of that control to Public.

Getting an Active Doc.

  1. Click the Notice.cs [Design] tab to access the first form
  2. Under the form, click mnuMain
  3. Under the Properties window, click Edit Items…
  4. In the Members list, click mnuFile
  5. In the right list, click DropDownItems and click its button
  6.  In the Select Item And Add To List Below, make sure MenuItem is selected and click Add
  7. Change the following characteristics:
    Text &Close
    (Name) mnuFileClose
    Enabled False
  8. Use the up button up to move and position it just under mnuFileOpen

 

 

3

 

  1. Click OK
  2. On the form, click File and double-click Close
  3. Implement the event as follows:
    private void mnuFileClose_Click(object sender, EventArgs e)
    {
        SingleDocument document = (SingleDocument)ActiveMdiChild;
    
        if( document  != null )
        {
            document.Close();
        }
    
        if (MdiChildren.Length== 0)
            mnuFileClose.Enabled = false;
    }
  4. Return to the form
  5. Save all

Minimized Children Organization:

In an MDI application, if the user doesn’t want to display a document but doesn’t want to close it, he can minimise the window. In the same way, the user can minimize as many child forms as necessary. When a child form has been minimized, it shows a button in the lower part of the parent. The buttons of the other minimized child forms are usually positioned next to each other:12

 

A user can also close the child form using the Close button of its minimized button. At one time the minimized buttons may display as a “mess”. To let you rearrange them, call the the LayoutMdi method of the Form class and pass the argument as ArrangeIcons. When you do this, the application will visit all buttons, if any, that represent minimized child documents, and position them from the left to the right, adjacently.

 

that’s all..Thanks…….