Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Manning - Windows.forms.programming.with.c#.pdf
Скачиваний:
108
Добавлен:
12.02.2016
Размер:
14.98 Mб
Скачать
Sys-

16.2.1CREATING AN MDI CONTAINER FORM

The creation of an MDI container form is much like the creation of any other form. Such a form is often referred to as a parent form, since it acts as the parent for one or more MDI child forms. The following table details the steps required for this task.

Set the version number of the MyPhotos application to 16.2.

CREATE A NEW FORM AS AN MDI CONTAINER

 

Action

Result

 

 

 

1

In the Solution Explorer window,

The new file appears in the Solution Explorer window and

 

add a new Windows Form to the

the ParentForm.cs [Design] window is displayed.

 

application called ParentForm.

 

 

 

 

2

Set the icon property for the

 

 

form to the “icons/Writing/

 

 

BOOKS04.ICO” file in the

 

 

common image directory.

 

 

 

 

3

Set the IsMdiContainer

 

 

property to true.

 

 

Note: This establishes the form

 

 

as an MDI container form.

 

 

 

 

4

Set the Size property to

 

 

600×400 pixels.

 

 

 

 

As you can see, the contents of the window appear in a darker color and includes a 3- D border to indicate that this form is now an MDI container. This color is the tem.AppWorkspace color, which is typically a darker version of the System.Control color. This background is a hidden MdiClient control, and cannot be manipulated in code as it is not exposed by the Form class. This background contains the MDI child forms, and is always last in the z-order. As a result, any controls added to the form will appear above this background, and therefore in front of any MDI children. Typically, controls added to an MDI container are docked to one edge of the parent form.

The code generated for our ParentForm class is much like other forms we have seen in this book. The InitializeComponent method generated by Visual Studio

.NET is as follows:

private void InitializeComponent()

{

//

// ParentForm

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(592, 373);

MDI FORMS

531

this.IsMdiContainer = true;

this.Name = “ParentForm”;

this.Text = “ParentForm”;

}

With the parent form created, we can turn our attention to the child form.

16.2.2CREATING AN MDI CHILD FORM

With our MDI container in place, we can add the infrastructure required for generating MDI child forms. This will consist of a menu bar and a New menu item. Fortunately, we already have our MainForm class available to act as the child form.

The following table shows how to create a child form in our application. As part of this task, we will add an Exit menu as well.

ADD ABILITY TO CREATE CHILD FORMS

 

 

 

Action

 

 

Result

 

 

 

 

 

 

 

1

Add a MainMenu object to the ParentForm

 

 

class in the ParentForm.cs [Design] window.

 

 

 

 

 

 

 

 

2

Add a top-level File menu containing the

 

 

three menu items as shown.

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

Menu

Property

 

Value

 

 

 

File

(Name)

 

menuFile

 

 

 

 

Text

 

&File

 

 

 

New

(Name)

 

menuNew

 

 

 

 

Shortcut

 

CtrlN

 

 

 

 

Text

 

&New

 

 

 

separator

 

 

 

 

 

 

 

Exit

(Name)

 

menuExit

 

 

 

 

Text

 

E&xit

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3

Add a Click event handler for the Exit

private void menuExit_Click

 

menu to close the form.

 

 

(object sender, System.EventArgs e)

 

 

 

 

 

 

 

{

 

 

 

 

 

 

 

Close();

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

4

Add a Click event handler for the New

private void menuNew_Click

 

menu.

 

 

 

 

(object sender, System.EventArgs e)

 

 

 

 

 

 

 

{

 

 

 

 

 

 

 

 

532

CHAPTER 16 MULTIPLE DOCUMENT INTERFACES

ADD ABILITY TO CREATE CHILD FORMS (continued)

 

Action

Result

 

 

 

5

Within this handler, create a MainForm

MainForm newChild = new MainForm();

 

object as an MDI child form.

newChild.MdiParent = this;

 

 

newChild.Show();

 

How-to

}

 

a. Create a new MainForm object.

 

 

b. Define this form as an MDI child by set-

 

 

ting the current form as its MDI parent.

 

 

c. Display the child form using the Show

 

 

method.

 

 

 

 

That’s all it takes to create a child form. You have almost created your first MDI application.

If you compile and run the application, you will note that the MyPhotos application runs exactly as before. This is because the MainForm.Main method is still the entry point for the application, and it displays the MainForm object using the Application.Run method. To fix this, we need to display the ParentForm class in the entry point for the application. This is our next subject.

16.2.3ADDING A NEW ENTRY POINT

One quite simple means to fix our entry point would be to modify the Main method in the MainForm class directly. The new code would look as follows, with the change highlighted in bold:

public class MainForm : System.Windows.Forms.Form

{

. . .

[STAThread]

static void Main()

{

Application.Run(new ParentForm());

}

. . .

}

While this code would do exactly what we want, a drawback of this change is that we could no longer compile the application as the single document interface we created in chapter 13. To preserve this ability, we will instead create a Main method as part of the ParentForm class, and modify the project to use this new method as the entry point.

The following table creates a new entry point within the ParentForm class.

MDI FORMS

533

CREATE AN ENTRY POINT IN THE PARENT FORM

 

Action

Result

 

 

 

1

Create a Main method in the

/// <summary>

 

ParentForm.cs code window to

/// Entry point for MDI application.

 

serve as the entry point for our

/// </summary>

 

[STAThread]

 

MDI application.

 

static void Main()

 

Note: If you compile the applica-

{

 

Application.Run(new ParentForm());

 

tion after this step, you will get an

 

}

 

error indicating that the program

 

 

defines more than one entry

 

 

point.

 

 

 

 

2

Set the Startup Object for the

 

 

MyPhotos project to the

 

 

MyPhotos.ParentForm class.

 

 

How-to

 

 

a. Display the Property Pages dia-

 

 

log for the project.

 

 

b. Click the down arrow associ-

 

 

ated with the Startup Object

 

 

entry.

 

 

c. Select the MyPhotos.Parent-

 

 

Form class.

 

 

 

 

The application is now ready. The startup object specified here is used by the C# compiler to establish the entry point for the application, and is only required if there are multiple Main methods in your project. On the command-line, the C# compiler accepts the /main switch to specify the class containing the Main method to use as the application’s entry point.

Run the application to verify that the ParentForm window appears and the New menu can be used to create MainForm objects as child windows. If you explore this new application, you will find some rather peculiar behavior for some of the controls. We will discuss and address these issues throughout the remainder of this chapter.

TRY IT! Of course, the MyPhotos Property Pages dialog used in step 2 can also be used to set the Startup Object to the MyPhotos.MainForm class. When this is done, the application displays the familiar single document interface created in part 2 of this book. Make this change and run the application to observe this behavior.

Among the odd features you may notice in the MDI version of this application is the menu bar. In particular, there are two File menus when a MainForm window is displayed. Adjusting this behavior is our next topic.

534

CHAPTER 16 MULTIPLE DOCUMENT INTERFACES

Соседние файлы в папке c#