Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Manning - Windows Forms Programming With CSharp.pdf
Скачиваний:
72
Добавлен:
24.05.2014
Размер:
14.98 Mб
Скачать

CREATE THE PASSWORD SECTION OF THE ALBUMEDITDLG FORM

 

 

 

 

 

 

 

Action

 

 

 

Result

 

 

 

 

 

 

 

 

 

 

1

 

In the AlbumEditDlg.cs [Design] window,

 

 

 

drag a CheckBox control from the Toolbox

 

 

 

window onto the form.

 

 

 

 

 

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Property

 

Value

 

 

 

 

(Name)

 

cbtnPassword

 

 

 

 

Text

 

 

Require &Password

 

 

 

 

 

 

 

 

 

 

 

 

 

2

 

Add a text box to receive the password,

 

 

 

and an additional label and text box to

 

 

 

confirm the password. Set the size and

 

 

 

position of these controls as shown in the

 

 

 

graphic.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

Control

 

Property

Value

 

 

 

First

 

(Name)

txtAlbumPwd

 

 

 

TextBox

 

 

 

 

 

 

 

 

 

 

 

 

 

Enabled

False

 

 

 

 

 

PasswordChar

*

 

 

 

 

 

Label

 

(Name)

lblConfirmPwd

 

 

 

 

 

 

Enabled

False

 

 

 

 

 

 

 

Text

Confir&m

 

 

 

 

 

 

 

 

 

Password

 

 

 

 

 

 

TextAlign

MiddleLeft

Note: Since the default value for the

 

 

Second

 

(Name)

txtConfirmPwd

CheckBox is unchecked, these controls are

 

 

TextBox

 

 

 

 

 

 

 

set to disabled by default. We will enable

 

 

 

 

 

Enabled

False

them when the user clicks the check box.

 

 

 

 

 

Also, notice the two different settings

 

 

 

 

PasswordChar

x

 

 

 

 

for PasswordChar used here. This is done

 

 

 

 

 

 

 

 

 

 

 

only for illustrative purposes. Generally,

 

 

 

 

 

 

 

 

 

 

 

you should use the same password charac-

 

 

 

 

 

 

 

 

 

 

 

ter for all controls in a window.

 

 

 

 

 

 

 

 

 

 

 

 

306

CHAPTER 9 BASIC CONTROLS

CREATE THE PASSWORD SECTION OF THE ALBUMEDITDLG FORM (continued)

 

Action

Result

 

 

 

3

Use the tab order view to assign the

 

 

TabIndex properties for the controls in

 

 

the dialog, using the order shown in the

 

 

graphic.

 

 

 

 

4

Add a CheckedChanged handler for the

private void cbtnPassword_CheckedChanged

 

CheckBox object to enable the controls

(object sender, System.EventArgs e)

 

when the box is checked.

{

 

// Enable pwd controls as required.

 

 

 

How-to

bool enable = cbtnPassword.Checked;

 

This is the default event for the CheckBox

txtAlbumPwd.Enabled = enable;

 

lblConfirmPwd.Enabled = enable;

 

control, so simply double-click on the

 

txtConfirmPwd.Enabled = enable;

 

control.

if (enable)

 

 

 

 

{

 

 

// Assign focus to pwd text box

 

 

txtAlbumPwd.Focus();

 

 

}

 

 

}

 

 

 

5

Add a Validating event handler to the

private void txtAlbumPwd_Validating

 

txtAlbumPwd control.

(object sender, System.

 

Note: The Validating and Validated

ComponentModel.CancelEventArgs e)

 

{

 

events allow custom validation to be per-

if (txtAlbumPwd.TextLength == 0)

 

formed on a control.

{

 

MessageBox.Show(this,

 

 

 

 

"The password for the album "

 

 

+ "cannot be blank",

 

 

"Invalid Password",

 

 

MessageBoxButtons.OK,

 

 

MessageBoxIcon.Error);

 

 

e.Cancel = true;

 

 

}

 

 

}

 

 

 

BUTTON CLASSES

307

CREATE THE PASSWORD SECTION OF THE ALBUMEDITDLG FORM (continued)

 

Action

Result

 

 

 

6

Add a ValidPasswords method to return

private bool ValidPasswords()

 

whether the two passwords match.

{

 

 

if ((cbtnPassword.Checked)

 

 

&& (txtConfirmPwd.Text

 

 

!= txtAlbumPwd.Text))

 

 

{

 

 

MessageBox.Show(this,

 

 

"The password and confirm "

 

 

+ "values do not match",

 

 

"Password Error",

 

 

MessageBoxButtons.OK,

 

 

MessageBoxIcon.Error);

 

 

return false;

 

 

}

 

 

return true;

 

 

}

 

 

 

This code demonstrates a couple of new concepts, such as setting the focus and validating the contents of a control. Let’s look at these changes in a bit more detail.

private void cbtnPassword_CheckedChanged

 

b Handle the

 

(object sender, System.EventArgs e)

 

CheckedChanged

{

 

event

 

 

// Enable the password controls as required

 

 

bool enable = cbtnPassword.Checked;

 

 

txtAlbumPwd.Enabled = enable;

 

 

lblConfirmPwd.Enabled = enable;

 

 

txtConfirmPwd.Enabled = enable;

 

 

if (enable)

{

// Assign focus to password control txtAlbumPwd.Focus();

}

}

cSet focus to txtAlbumPwd control

private void txtAlbumPwd_Validating

(object sender, System.ComponentModel.CancelEventArgs e)

{

if (txtAlbumPwd.TextLength == 0)

{

MessageBox.Show(this,

"The password for the album cannot be blank", "Invalid Password",

MessageBoxButtons.OK,

MessageBoxIcon.Error);

e.Cancel = true;

}

}

private bool ValidPasswords()

{

Handle the d

Validating

event

308

CHAPTER 9 BASIC CONTROLS

if ((cbtnPassword.Checked)

&& (txtConfirmPwd.Text != txtAlbumPwd.Text))

{

MessageBox.Show(this,

"The password and confirm values do not match", "Password Error",

MessageBoxButtons.OK,

MessageBoxIcon.Error);

return false;

}

return true;

}

The numbered sections in this code warrant the following commentary.

bThe AutoCheck property handles the Click event automatically on behalf of our CheckBox control. To process the change in button state that occurs when this happens, we handle the CheckedChanged event. The value of the Checked property is used to enable or display the associated controls, as required.

cWhen our radio button is checked, the focus, by default, would remain with the cbtnPassword control. Typically, when a user checks this button, he or she would immediately want to edit the password field. Calling the Focus method does this automatically and saves the user an extra step.

dThe Validating event is one of a series of events related to entering and leaving a control. Collectively, these events are sometimes referred to as the focus events. The

focus events, in the order in which they occur, are as follows: Enter, GotFocus,

Leave, Validating, Validated, and LostFocus.These events can be used to fine-tune the behavior of a control as the user moves from one control to the next.

The validation events, namely Validating and Validated, occur during and after validation whenever the CausesValidation property is set to true. This property defaults to true, so the validation events normally occur. The Validating event receives a CancelEventArgs parameter much like the OnClosing event we discussed for the Form class in chapter 6. The CancelEventArgs.Cancel property is used to cancel the operation when the validation fails.

In our case, we want to verify that the password provided is not blank. When this occurs, we display a message box to inform the user of the problem, and cancel the operation to indicate that validation has failed. The .NET Framework returns focus to the control, forcing the user to correct the problem.

BUTTON CLASSES

309