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

CREATE OVERRIDABLE METHODS FOR OK AND RESET BUTTONS (continued)

 

Action

Result

 

 

 

9

Override the OnClosing method for

protected override void OnClosing

 

the form to invoke this new method

(CancelEventArgs e)

 

when the user clicks the OK button.

{

 

if (!e.Cancel && (this.DialogResult

 

 

 

Note: This method is discussed in

== DialogResult.OK))

 

detail in chapter 8. Note how the set-

{

 

e.Cancel = ! SaveSettings();

 

tings are saved only if a subclass has

 

}

 

not cancelled the operation.

base.OnClosing(e);

 

 

 

 

}

 

 

 

The ResetSettings and SaveSettings methods are now available to our derived forms. Compile your code to make the base form available for inheritance.

Next, let’s create a derived form for editing a photograph’s settings. The EditDlg form will act as the parent of this new form.

9.1.2Creating a derived form

A new form is derived from an existing form the same way that any new class is derived from an existing class. The base form is defined as the parent class of the new form.

public class PhotoEditDlg : Manning.MyPhotoAlbum.BaseEditDlg

{

// class definition goes here

}

In our case, we will create a derived form and leave the addition of new members for the subsequent sections. Visual Studio supports the creation of inherited forms graphically via an Add Inherited Form… menu in the Project menu, or the context menu of the project itself. This is detailed in the following steps.

DERIVE THE PHOTOEDITDLG FORM FROM THE BASEEDITDLG FORM

 

Action

Result

 

 

 

1

Open the Add New Item dialog to

The Add New Item dialog displays with the Inherited

 

add a new PhotoEditDlg form

Form template selected by default.

 

inherited from the existing

 

 

BaseEditDlg form.

 

 

How-to

 

 

a. In the Solution Explorer window,

 

 

right-click on the MyPhotoAlbum

 

 

project.

 

 

b. Select Add Inherited Form…

 

 

from the Add menu.

 

 

c. Enter the name “PhotoEditDlg.”

 

 

 

 

FORM INHERITANCE

269

DERIVE THE PHOTOEDITDLG FORM FROM THE BASEEDITDLG FORM (continued)

 

Action

Result

 

 

 

2

Click the Open button to display the

This window is shown in the next step.

 

Inheritance Picker dialog.

 

 

 

 

3

Define BasedEditDlg as the base

 

 

class for the new form.

 

 

Note: If you get an error here, it

 

 

likely means that your BaseEdit-

 

 

Dlg form was never compiled.

 

 

Visual Studio looks for inheritable

 

 

forms in the existing assembly, so

 

 

you must compile before you can

 

 

inherit.

 

 

 

 

4

Click the OK button in the

A new file PhotoEditDlg.cs is added to the project and

 

Inheritance Picker dialog to create

the PhotoEditDlg.cs [Design] window is displayed.

 

the class file and add it to the

 

 

MyPhotoAlbum project.

 

 

Settings

 

 

Set the Text property to

 

 

“PhotoEditDlg” to distinguish this

 

 

window from our base form.

 

 

 

Note: Notice the small graphic on the existing

 

 

controls here. This graphic indicates that these

 

 

controls are inherited by the form.

 

 

 

View the code generated in the PhotoEditDlg.cs file, an excerpt of which follows. You will note that the new class is based on the BaseEditDlg class, and does not yet contain any controls of its own.

namespace Manning.MyPhotoAlbum

{

public class PhotoEditDlg : Manning.MyPhotoAlbum.BaseEditDlg

{

private System.ComponentModel.IContainer components = null;

. . .

#region Designer generated code

270

CHAPTER 9 BASIC CONTROLS

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