Creating MDI Applications

So far in this chapter, you have created only single-document interface (SDI) applications. These applications allow a user to work with only one window at a time. Several large Windows applications, such as Microsoft Excel and Visual Studio .NET, allow users to work with several open windows at the same time. These applications are called multiple-document interface (MDI) applications. The main application window of an MDI application acts as the parent window, which can open several child windows. You need to know the following main points about an MDI application:

  • Child windows are restricted to their parent window (that is, you cannot move a child window outside its parent window).

  • The parent window can open several types of child windows. As an example of an MDI application, Visual Studio .NET allows you to work with several types of document windows at the same time.

  • The child windows can be opened, closed, maximized, or minimized independently of each other, but when the parent window is closed, the child windows are automatically closed.

  • The MDI frame should always have a menu; one of the menus that a user always expects to see in an MDI application is the Window menu (see Figure 2.44), which allows the user to manipulate various windows that are open in an MDI container form.

    Figure 2.44. An MDI application typically has a Window menu item.

The Windows Forms in an MDI application are also created by using the standard System.Windows.Forms.Form class. To create an MDI parent form, you create a regular Windows form and change its IsMdiContainer property to true. To create an MDI child form, you create a regular Windows form and assign the name of the parent MDI object to the MdiParent property. Table 2.33 summarizes the important members of the Form class that are related to the MDI applications.

Table 2.33. Important Members of the Form Class That Are Related to MDI Applications
MemberTypeDescription
ActiveMdiChildPropertyIdentifies the currently active MDI child window
IsMdiContainerPropertyIndicates whether the form is a container for MDI child forms
MdiChildActivateEventOccurs when anMDI child form is activated or closed within an MDI application
MdiChildrenPropertySpecifies an array of forms that represent the MDI child form of the form
MdiParentPropertySpecifies the MDI parent form for the current form
LayoutMdi()MethodArranges the MDI child forms within an MDI parent form

Step by Step 2.22 shows how to create an MDI application. In it you can create a form that is similar to the one created in Step by Step 2.18 and you can use it as an MDI child window.

You learn the following from Step by Step 2.22:

  • How to create an MDI parent form

  • How to create MDI child forms

  • How to convert an existing SDI application to an MDI application

  • How to merge menus between MDI parent and child windows

  • How to create Windows menus that allow users to load and rearrange MDI child forms

STEP BY STEP

2.22 Creating an MDI Application

1.
Add a Windows form to existing project 316C02. Name the form MdiChild.

2.
Follow steps 2 to 27 from Step by Step 2.18.

3.
Select the main menu of the form and change the MergeType property of the File menu to MergeItems. Select the File, New and File, Open menus and change their MergeType properties to Remove.

4.
Rename the File, Exit menu item &Close. Change the MergeOrder properties for the File, Save As; File, Close; Format; and Help menu items to 5, 3, 10, and 30, respectively. Change the MergeOrder property for the separator in the File menu to 4.

5.
Switch to the code view. After the default constructor code, add another constructor with the following code:

public MdiChild(string fileName)
{
    //
    // Required for Windows Forms Designer support
    //
    InitializeComponent();
    rtbText.LoadFile(fileName, 
        RichTextBoxStreamType.RichText);
}

6.
Add another Windows form to existing project 316C02. Name the form StepByStep2_22 and change its IsMdiContainer property to true.

7.
Add a MainMenu component to the form. Create a top-level menu item, change its Text property to &File, and name it mnuFile. Add the following menu items to it: &New (mnuFileNew), &Open... (mnuFileOpen), separator, and E&xit (mnuFileExit). Change the menu items' MergeOrder properties to 1, 2, 6, and 7, respectively.

8.
Add another top-level menu. Change the Text property to &Window, and the Name property to mnuWindow, and the MdiList property to true. Add the following menu items to it: Tile &Horizontally (mnuWindowTileHorizintally), Tile &Vertically (mnuWindowTileVertically), and &Cascade (mnuWindowCascade).

9.
Double-click the File, New menu item and add the following event handler to its Click event:

private void mnuFileNew_Click(
    object sender, System.EventArgs e)
{
    //create a new instance of child window
    MdiChild frmMdiChild = new MdiChild();
    //set its MdiParent
    frmMdiChild.MdiParent = this;
    frmMdiChild.Text = "New Document";
    //Display the child window
    frmMdiChild.Show();
}

10.
Double-click the File, Open menu item and add the following event handler to it:

private void mnuFileOpen_Click(
    object sender, System.EventArgs e)
{
    //Allow to select only *.rtf files
    dlgOpenFile.Filter = 
        "Rich Text Files (*.rtf)|*.rtf";
    if(dlgOpenFile.ShowDialog() == DialogResult.OK)
    {
        //create the child form by 
        //loading the given file in it
        MdiChild frmMdiChild = 
           new MdiChild(dlgOpenFile.FileName);
        //Set the current for as its parent
        frmMdiChild.MdiParent = this;
        //Set the file's title bar text
        frmMdiChild.Text = dlgOpenFile.FileName;
        //display the form
        frmMdiChild.Show();
    }
}

11.
Add the following event handlers to Click events of other menu items, as shown in steps 9 to 10:

private void mnuFileExit_Click(
     object sender, System.EventArgs e)
{
    //Close the parent window
    this.Close();
}
private void mnuWindowTileHorizontally_Click(
     object sender, System.EventArgs e)
{
    //Tile child windows horizontally
    this.LayoutMdi(MdiLayout.TileHorizontal);
}
private void mnuWindowTileVertically_Click(
    object sender, System.EventArgs e)
{
    //Tile child windows vertically
    this.LayoutMdi(MdiLayout.TileVertical);
}
private void mnuWindowCascade_Click(
    object sender, System.EventArgs e)
{
    //cascade 
    this.LayoutMdi(MdiLayout.Cascade);
}

12.
Add thefollowing event handler to the Popup event of the mnuWindow menu item:

private void mnuWindow_Popup(
    object sender, System.EventArgs e)
{
    //code to enable and disable Window menu items
    //depending on if any child windows are open
    if (this.MdiChildren.Length > 0)
    {
        mnuWindowTileHorizontally.Enabled = true;
        mnuWindowTileVertically.Enabled = true;
        mnuWindowCascade.Enabled = true;
    }
    else
    {
        mnuWindowTileHorizontally.Enabled = false;
        mnuWindowTileVertically.Enabled = false;
        mnuWindowCascade.Enabled = false;
    }
}

13.
Insert the Main() method to launch form StepByStep2_22. Set the form as the startup object for the project.

14.
Run the project. From the File menu, open a new document, and then also open an existing document by selecting File, Open. Click the Window menu and select various options to arrange the child windows (see Figure 2.45).

Figure 2.45. When you are working with an MDI application, you can use the commands from the Window menu to switch between windows or documents.



The default MergeType property for the MenuItem objects is Add. This means that the MenuItem objects in Step by Step 2.22 are added together on an MDI parent window. If you don't want to include some of the menu items, you can set their MergeType properties to Remove. The MergeOrder properties for the MenuItem objects specify the order in which they appear on the parent MDI form.

There is another interesting property of the MenuItem object that is used with MDI applications: the MdiList property. When this property is set to true, the MenuItem object is populated with a list of MDI child windows that are displayed within the associated form.

REVIEW BREAK

  • There are two primary types of menus. The main menu is used to group all the available commands and option in a Windows application, and a context menu is used to specify a relatively smaller list of options that apply to a control, depending on the application's current context.

  • You can make keyboard navigation among menu items possible by associating hotkeys in the Text property of the menu items. You can also associate shortcut keys, which directly invoke commands, with a menu.

  • The Clipboard object consists of two public static methods, GetDataObject() and SetDataObject(), which get and set the data from the Clipboard.

  • The StatusBar control creates a standard Windows status bar in an application. You can use StatusBar to display various messages and help text to the user.

  • You can use toolbars to create a set of small buttons that are identified by icons. Toolbars generally provide shortcuts to operations that are available in the application's main menu. Toolbars are common in Windows applications and make an application simple to use.

  • An MDI application allows multiple documents or windows to be open at the same time.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.210.166