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

CREATE A PASSWORD DIALOG

 

 

 

Action

 

 

Result

 

 

 

 

 

 

 

4

Create a new Windows Form called

The PasswordDlg.cs file is created and added to

 

PasswordDlg in the MyPhotoAlbum

the project, and its design window is displayed.

 

project.

 

 

 

 

 

 

 

 

 

 

 

5

Create the form as shown, using the

 

 

following settings.

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

Control

Property

Value

 

 

 

Label

Text

as shown

 

 

 

TextBox

(Name)

txtPassword

 

 

 

 

Password-Char

*

 

 

 

 

Button

(Name)

btnOk

 

 

 

 

DialogResult

OK

Note: The PasswordChar property setting for

 

 

 

Text

&OK

 

 

 

the TextBox control masks the user’s entry

 

 

Form

AcceptButton

btnOk

 

 

with the given character. When this property

 

 

 

ControlBox

False

 

 

 

is set, the Clipboard cut and copy operations

 

 

 

FormBorderStyle

FixedDialog

are disabled (the paste operation is still per-

 

 

 

ShowInTaskbar

False

mitted).

 

 

 

Size

262, 142

 

 

 

 

 

Text

Enter

 

 

 

 

 

Password

 

 

 

 

 

 

 

 

6

Create a Password property to retrieve

public string Password

 

the value of the txtPassword control.

{

 

 

 

 

 

 

get { return txtPassword.Text; }

 

 

 

 

 

 

}

 

 

 

 

 

 

 

We will use this dialog to request a password when an album is opened. The point here is to illustrate the PasswordChar property, and not to create a secure mechanism for handling passwords.

ENFORCE (INSECURE) PASSWORD MECHANISM

 

Action

Result

 

 

 

7

In the PhotoAlbum.cs code

using System.Windows.Forms;

 

window, indicate that this

 

 

class uses the Windows

 

 

Forms namespace.

 

 

 

 

BUTTON CLASSES

297

ENFORCE (INSECURE) PASSWORD MECHANISM (continued)

 

Action

Result

 

 

 

8

Use the new PasswordDlg

public void Open(string fileName)

 

form in the

{

 

PhotoAlbum.Open method

. . .

 

try

 

to receive a password from

 

{

 

the user.

. . .

 

 

// Check for password

 

 

if (_password != null && _password.Length > 0)

 

 

{

 

 

using (PasswordDlg dlg = new PasswordDlg())

 

 

{

 

 

dlg.Text = String.Format(

 

 

"Opening album {0}",

 

 

Path.GetFileName(_fileName));

 

 

if ((dlg.ShowDialog() == DialogResult.OK)

 

 

&& (dlg.Password != _password))

 

 

{

 

 

throw new ApplicationException(

 

 

"Invalid password provided");

 

 

}

 

 

}

 

 

}

 

 

. . .

 

 

}

 

 

 

Our PhotoAlbum class is now ready. Each PhotoAlbum instance supports the new title, password, and display option settings. These settings are saved in the album file, and in the Open method an album cannot be loaded unless the proper password is provided.3

Let’s get back to the matter at hand and create our Album Properties dialog.

9.3.3CREATING THE ALBUMEDITDLG PANEL AREA

With our new infrastructure in place, we are ready to create the AlbumEditDlg form. This section will inherit the new dialog from our base form, and add controls to the panel at the top of the form.

3 Of course, the password mechanism here is quite insecure. A user can examine the album file by hand in order to discern the password. We could make this scheme more secure by using the password as a scrambling mechanism on the file. For example, rather than storing the password string in the file, call GetHashCode on the password string and XOR each character in the file with this hash code and the byte-sum of the password characters. The scrambled result is then stored in the file. The validity of the password is checked by using it to unscramble the version number of the file to see if it makes sense. If so, then the password is presumed to be valid. Again, this is not a totally secure mechanism, but it is slightly better then that shown in the text. For more information on security in .NET in general and the System.Security namespace in particular, see the book .NET Security by Tom Cabanski from Manning Publications.

298

CHAPTER 9 BASIC CONTROLS

The Panel control inherited from the BaseEditDlg form will display the album file and the title in our new AlbumEditDlg form. The following steps create the new form class and the controls contained by the panel.

CREATE THE ALBUMEDITDLG FORM AND ITS PANEL CONTROLS

 

 

 

 

Action

 

 

Result

 

 

 

 

 

 

 

 

1

Derive a new AlbumEditDlg form

 

 

from the BaseEditDlg form.

 

 

 

 

 

Settings

 

 

 

 

Set the Text property for the new

 

 

dialog to “Album Properties.”

 

 

 

 

 

 

 

 

 

2

Add two labels to the left side of

The new controls are contained by the Panel control,

 

the form.

 

 

 

rather than by the Form object.

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

Label

 

Property

Value

 

 

 

label1

 

Text

Album &File

 

 

 

 

 

TextAlign

MiddleRight

 

 

 

label2

 

Text

&Title

 

 

 

 

 

TextAlign

MiddleRight

 

 

 

 

 

 

 

 

 

3

Add the two text boxes to the

 

 

form. Resize and position the

 

 

controls and the panel as shown

 

 

in the graphic.

 

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

TextBox

Property

Value

 

 

 

Album File

(Name)

txtAlbumFile

 

 

 

 

 

ReadOnly

True

 

 

 

 

 

Text

 

 

 

 

 

Title

(Name)

txtTitle

 

 

 

 

 

Text

 

 

 

 

 

 

 

 

 

 

 

BUTTON CLASSES

299

With these steps completed, we are ready to add our button controls.

9.3.4USING RADIO BUTTONS

The bottom part of our dialog will contain some radio and check box buttons. We will begin with the radio buttons in the middle of the form.

Radio buttons display a set of mutually exclusive options. In our case, they indicate which Photograph setting should be used to represent the photograph in a main window. Radio buttons in .NET work much the same as radio buttons in other graphical environments. An overview of this class appears in .NET Table 9.6.

In our code, three radio buttons will collectively set the _displayOption setting for the album. Since these are the only radio buttons on the form, it is not necessary to group them within a container control. We will anyway, since it improves the overall appearance of the form.

.NET Table 9.6 RadioButton class

The RadioButton class represents a button that displays one of a possible set of options. Radio buttons are usually grouped together, and only one button may be checked at any one time. By default, when a radio button is clicked, it is automatically selected and all radio buttons in the same group are deselected. This behavior can be disabled using the AutoCheck property.

The parent container for this control defines its group. So if four radio buttons are contained within a form, then only one of the four buttons may be checked at any one time. Use container classes such as GroupBox and Panel to provide multiple independent groups of radio buttons on a single form.

This control is part of the System.Windows.Forms namespace, and inherits from the ButtonBase class. See .NET Table 9.4 on page 292 for members inherited from this class.

 

Appearance

Gets or sets whether the control appears as a

 

 

normal radio button or as a toggle button.

 

AutoCheck

Gets or sets the behavior of related radio buttons

 

 

when this button is clicked. If true, then the

 

 

framework automatically deselects all radio

Public Properties

 

buttons in the same group; if false, other radio

 

buttons in the same group must be deselected

 

 

 

 

manually.

 

CheckAlign

Gets or sets the alignment of the click box

 

 

portion of the control.

 

Checked

Gets or sets whether the control is checked.

 

 

 

Public Methods

PerformClick

Sends a Click event to the control.

 

 

 

 

AppearanceChanged

Occurs when the Appearance property

Public Events

 

changes.

 

 

 

CheckedChanged

Occurs when the Checked property changes.

 

 

 

A Panel control could be used here, of course, but this is a good opportunity to create a GroupBox control. The GroupBox class inherits directly from the Control class to

300

CHAPTER 9 BASIC CONTROLS

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