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

Possible values for the Modifiers property

Value

C# equivalent

Comments for Form inheritance

 

 

 

Public

public

Any class, regardless of where and how it is defined, can modify

 

 

the control. This is not typically used, since you do not normally

 

 

want any object to modify the location, size, or other internal

 

 

control settings of your form.

Protected

protected

Any subclass of the form, regardless of where it is defined, can

 

 

modify the control.

Protected

protected

Any subclass of the form that is defined in the same assembly

Internal

internal

can modify the control.

Internal

internal

Any class in the same assembly, regardless of how it is defined,

 

 

can modify the control. This is safer than public access, since

 

 

you typically have control over the classes common to an

 

 

assembly.

Private

private

No subclass can modify the control. This is the default setting.

 

 

 

Based on the table, we could have used either the Protected or Protected Internal setting here. Since there is no reason to prevent derived forms in external assemblies from modifying the Panel control, the Protected value will work just fine.

Before we move on, notice that our subclasses will not be able to add Click handlers for our private buttons. The OK and Cancel buttons have assigned actions due to their DialogResult setting. When either button is clicked, the dialog is deactivated and the appropriate value returned. We will require a way to save our modified settings when the OK button is clicked, and we need a way to perform an action when the Reset button is clicked.

As a solution, let’s add two protected methods that child classes can implement to handle these situations. We will create a SaveSettings method to store the modified values, and a ResetSettings method to handle a click of the Reset button. This continues our previous steps.

CREATE OVERRIDABLE METHODS FOR OK AND RESET BUTTONS

 

Action

Result

 

 

 

6

Create a protected virtual method for

protected virtual void ResetSettings()

 

resetting the form.

{

 

 

// Subclasses override to reset form

 

 

}

 

 

 

7

Add a Click handler for the Reset

private void btnReset_Click

 

button to invoke this new method.

(object sender, System.EventArgs e)

 

 

{

 

 

ResetSettings();

 

 

}

 

 

 

8

Create a protected virtual method for

protected virtual bool SaveSettings()

 

saving the dialog settings when a

{

 

form is deactivated. This should return

// Subclasses override to save form

 

return true;

 

whether the save was successful.

 

}

 

 

 

268

CHAPTER 9 BASIC CONTROLS

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