Building MDI WinForms Application Using C#

August 8th, 2013 | Posted by Vidya Vrat in .NET | C#

Implementing an MDI Form

The Multiple-Document Interface (MDI) is a specification that defines a user interface for applications that enable the user to work with more than one document at the same time under one parent form (window).

Visualize the working style of an application in which you are allowed to open multiple forms in one parent container window, and all the open forms will get listed under the Windows menu.  Whereas having an individual window for each instance of the same application is termed as single document interface (SDI); applications such as Notepad, Microsoft Paint, Calculator, and so on, are SDI applications. SDI applications get opened only in their own windows and can become difficult to manage, unlike when you have multiple documents or forms open inside one MDI interface.

Hence, MDI applications follow a parent form and child form relationship model. MDI applications allow you to open, organize, and work with multiple documents at the same time by opening them under the context of the MDI parent form; therefore, once opened, they can’t be dragged out of it like an individual form.

The parent (MDI) form organizes and arranges all the child forms or documents that are currently open. You might have seen such options in many Windows applications under a Windows menu, such as Cascade, Tile Vertical, and so on.

Try It: Creating an MDI Parent Form with a Menu Bar

In this exercise, you will create an MDI form in the WinApp project. You will also see how to create a menu bar for the parent form, that will allow you to navigate to all the child forms. To do so, follow these steps:

  1. Navigate to Solution Explorer, select the WinApp project, right-click, and select “Add” -> “Windows form”. Change the Name value from “Form1.cs” to “ParentForm.cs”, and click “Add”.
  2. Select the newly added ParentForm in the Design View. Select the ParentForm form by clicking the form’s title bar, navigate to the Properties window, and set the following properties:
    • Set the “IsMdiContainer” property to True (the default value is False). Notice that the background color of the form has changed to dark gray.
    • Set the Size property’s Width to 546 and Height to 411.

 

  1. Drag a MenuStrip control to the ParentForm. In the top-left corner, you should now see a drop-down showing the text “Type Here”. Enter the text “Open Forms” in the drop-down. This will be your main, top-level menu.
  2. Now under the Open Forms menu, add a submenu by entering the text “Win App”.
  3. Under the Win App submenu, enter “User Info”.
  4. Now click the top menu, “Open Forms”, and on the right side of it, type “Help”. Under the Help menu, enter “Exit”.
  5. Now, click the top menu, on the right side of Help, type “Windows”.
  6. Under the Windows menu, add the following options as separate submenus: Cascade, Tile Horizontal, Tile Vertical, and Arrange Icons. These will help in arranging the child forms.
  7. Now it’s time to attach code to the submenus you have added under the main menu Open Forms. First, you’ll add code for the submenu Win App, that basically will open the WinApp form. In the Design View, double-click the “Win App” submenu, that will take you to the Code View. Under the click event, add the following code:

WinApp objWA = new WinApp();
objWA.Show();

  1. Now to associate functionality with the User Info submenu: double-click this submenu, and under the click event add the following code:

UserInfo objUI = new UserInfo();
objUI.Show();

  1. To associate functionality with the Exit submenu located under the Help main menu, double-click “Exit”, and under the click event add the following code:

Application.Exit();

  1. Now you have the form-opening code functionality in place, and you are nearly set to run the application. But first, you need to set the ParentForm as the start-up object. To do so, open Program.cs, and modify the “Application.Run(new UserInfo());” statement to the following:

Application.Run(new ParentForm());

  1. Now build the solution, and run the application by pressing F5; the MDI application will open and should look as in Figure 1-1.

Figure 1-1. Running an MDI form application

  1. Now if you click “Win App” and then “User Info” then both the forms will open one by one. These forms can be opened and dragged outside of the MDI form. This is not an expected behavior from a MDI application, as shown in Figure 1-2.

This issue will be addressed later in this chapter.

Figure 1-2. Running an MDI form application

How It Works

Each Windows Forms form is a class and exposes a Show() function by an instance created for it. You use the following code, that is creating an object and then invoking the Show() method. This opens the other form from the MDI parent form.

This creates an instance of the WinApp form and opens it for you:

WinApp objWA = new WinApp();
objWA.Show();

The following code creates an instance of the UserInfo form and opens it for you:

UserInfo objUI = new UserInfo();

Read Full Article Here

 

 

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.