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

displays descriptive text about the photo, and automatically scrolls if the text becomes too long. The following steps add this control to our dialog:

ADD A MULTILINE TEXTBOX TO THE PHOTOEDITDLG FORM

 

 

 

Action

 

 

 

Result

 

 

 

 

 

 

 

 

1

 

Add the Notes label to the

The AutoSize property causes the label to resize to exactly fit

 

 

PhotoEditDlg form in the

its Text value.

 

 

PhotoEditDlg.cs [Design]

 

 

 

window..

 

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

Property

 

Value

 

 

 

 

AutoSize

 

True

 

 

 

 

TabIndex

 

4

 

 

 

 

 

 

Text

 

Notes:

 

 

 

 

 

 

 

 

 

 

2

 

Add the multiline TextBox

 

 

 

control to the form.

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Property

 

Value

 

 

 

 

(Name)

 

txtNotes

 

 

 

 

AcceptsReturn

 

True

 

 

 

 

Multiline

 

True

 

 

 

 

ScrollBars

 

Vertical

 

 

 

 

TabIndex

 

5

 

 

 

 

 

 

Text

 

 

 

 

 

 

 

 

 

 

 

 

 

Note: The Multiline property must be set to true

 

 

 

 

 

 

 

 

before the control can be resized to contain multiple lines

 

 

 

 

 

 

 

 

of text.

 

 

 

 

 

 

 

 

The AcceptsReturn property causes the control to

 

 

 

 

 

 

 

 

treat an Enter key as a new line rather than allowing the

 

 

 

 

 

 

 

 

parent form to invoke the OK button.

 

 

 

 

 

 

 

 

 

Our form is now ready, except for the internal logic to process the user’s changes. Since our dialog is intended to edit a Photograph object within a PhotoAlbum

collection, we need a reference to the associated PhotoAlbum object within the dialog. We should also implement the methods necessary to handle the OK and Reset buttons, namely the ResetSettings and SaveSettings methods provided by the

BaseEditDlg class.

The following steps detail these changes:

LABELS AND TEXT BOXES

283

INTERACTING WITH THE PHOTOALBUM OBJECT

 

Action

Result

 

 

 

3

In the PhotoEditDlg.cs file add a

private PhotoAlbum _album;

 

private PhotoAlbum variable to

 

 

hold the album containing the

 

 

photo to display.

 

 

 

 

4

Modify the constructor to accept

public PhotoEditDlg(PhotoAlbum album)

 

a PhotoAlbum parameter.

{

 

 

 

5

Within the constructor, set the

// This call is required . . . .

 

album variable and call

InitializeComponent();

 

ResetSettings to initialize the

// Initialize the dialog settings

 

dialog’s controls.

 

_album = album;

 

 

ResetSettings();

 

 

}

 

 

 

6

Implement the ResetSettings

protected override void ResetSettings()

 

method to set the controls to

{

 

their corresponding settings in

Photograph photo = _album.CurrentPhoto;

 

 

 

the current photograph.

if (photo != null)

 

 

{

 

 

txtPhotoFile.Text = photo.FileName;

 

 

txtCaption.Text = photo.Caption;

 

 

txtDate.Text

 

 

= photo.DateTaken.ToString();

 

 

txtPhotographer.Text = photo.Photographer;

 

 

this.txtNotes.Text = photo.Notes;

 

 

}

 

 

}

 

 

 

7

Implement SaveSettings to

protected override bool SaveSettings()

 

save the contents of the form to

{

 

the current photograph.

Photograph photo = _album.CurrentPhoto;

 

 

 

Note: Here, the settings are

if (photo != null)

 

always stored successfully, so

{

 

photo.Caption = txtCaption.Text;

 

this method always returns

 

// Ignore txtDate setting for now

 

true.

photo.Photographer = txtPhotographer.Text;

 

 

photo.Notes = txtNotes.Text;

 

 

}

 

 

return true;

 

 

}

 

 

 

Our dialog is complete, at least for now. Applications can use it to display and modify information about a photograph. The one exception is the date a photograph was taken. While it is certainly possible to convert a string provided by the user into a DateTime structure, this is not really the best way to specify a date on a form. Instead, the DateTimePicker control is provided especially for this purpose. We will look at this control in chapter 11, and simply ignore the value of txtDate for now.

The next step is to use this new dialog in our main application. This is the topic of the next section.

284

CHAPTER 9 BASIC CONTROLS

9.2.4ADDING PHOTOEDITDLG TO OUR MAIN FORM

Now that our new dialog is ready, we need to display it in our MyPhotos application.

This section integrates the dialog into our application, much like we integrated the CaptionDlg form in chapter 8.

The CaptionDlg form does present a slight problem, in that it already allows the caption to be edited, just like our new PhotoEditDlg form. We could keep this dialog around and provide two ways to edit a photograph’s caption. This might be a little confusing to users, so we will instead remove CaptionDlg from our application.

The step to remove this dialog follows. We will integrate the PhotoEditDlg dialog into our application in a moment.

Set the version number of the MyPhotos application to 9.2.

 

REMOVE THE CAPTIONDLG FORM

 

 

 

 

Action

Result

 

 

 

1

In the Solution Explorer window, delete

 

 

the CaptionDlg form.

 

 

How-to

 

 

a. Right-click on the CaptionDlg.cs file.

 

 

b. Select Delete from the popup menu.

 

 

c. Click OK in the confirmation box.

 

 

Alternately

After clicking OK, the CaptionDlg.cs class is

 

Click on the file and press the Delete key.

removed from the project and deleted from the

 

 

file system.

 

 

 

With the caption dialog gone, our way is clear to display the PhotoEditDlg form from our main window. We will reuse the menuCaption menu for this purpose, renamed and revamped by the following steps:

LABELS AND TEXT BOXES

285

DISPLAY THE PHOTOEDITDLG FORM FROM THE MAIN WINDOW

 

 

 

Action

Result

 

 

 

 

 

 

2

Double-click the MainForm.cs file in

The Windows Forms Designer window appears for

 

the Solution Explorer window.

this form.

 

 

 

 

 

 

3

Modify the properties for the Caption

 

 

menu item under the Edit menu.

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

Property

 

Value

 

 

 

(Name)

 

menuPhotoProp

 

 

 

Text

 

&Photo

 

 

 

 

 

Properties…

 

 

Note: We could elect to use this

 

 

menu under its previous name. This

 

 

could prove confusing in the future, so

 

 

we instead rename the control in line

 

 

with its new purpose.

 

 

 

 

 

 

 

4

Rename the Click event for this

 

 

menu to menuPhotoProp_Click.

 

 

 

 

 

 

 

5

Replace the old handler with an

private void menuPhotoProp_Click

 

implementation to display the

(object sender, System.EventArgs e)

 

PhotoEditDlg form.

{

 

if (_album.CurrentPhoto == null)

 

 

 

 

 

 

 

Note: The old handler was called

return;

 

menuCaption_Click.

using (PhotoEditDlg dlg

 

 

 

 

 

 

 

 

 

 

 

 

= new PhotoEditDlg(_album))

 

 

 

 

 

 

{

 

 

 

 

 

 

if (dlg.ShowDialog()

 

 

 

 

 

 

== DialogResult.OK)

 

 

 

 

 

 

{

 

 

 

 

 

 

_bAlbumChanged = true;

 

 

 

 

 

 

sbpnlFileName.Text

 

 

 

 

 

 

= _album.CurrentPhoto.Caption;

 

 

 

 

 

 

statusBar1.Invalidate();

 

 

 

 

 

 

}

 

 

 

 

 

 

}

 

 

 

 

 

 

}

 

 

 

 

 

 

6

Update the Popup event handler for

private void menuEdit_Popup

 

the Edit menu to use the new menu.

(object sender, System.EventArgs e)

 

 

 

 

 

 

{

 

 

 

 

 

 

menuPhotoProp.Enabled

 

 

 

 

 

 

= (_album.Count > 0);

 

 

 

 

 

 

}

 

 

 

 

 

 

 

Since the dialog itself handles the initialization and storage of any changes made by the user, and the using statement disposes of the dialog when we are finished, there is not much work required by our handler. When the user clicks OK, we mark that the album has changed and update the status bar with any new caption entered by the user.

286

CHAPTER 9 BASIC CONTROLS

So let’s see if your code actually works. Compile and run the application and open a previously saved album file. Display the Photo Properties dialog. Note in particular the following features:

The differences between the read-only and editable text boxes.

Label text cannot be highlighted, while text within text boxes can, even when read-only.

Use the access key for a label and notice how the following text box receives focus.

Press the Enter key while editing a single-line text box. The dialog behaves as if you had clicked the OK button.

Press the Enter key while editing within the Notes text box. Since we set the AcceptsReturn property to true, this adds a new line within the Notes box and does not deactivate the window.

Right-click on any text box. The default context menu will appear. This context menu contains various commands for editing text, and is shown in figure 9.3. The items in this menu correspond to methods in the TextBoxBase class, as shown in .NET Table 9.2.

While our form is working just fine, there are some features missing that might make our dialog a little more friendly. These are the subject of the next section.

Figure 9.3

The standard context menu for TextBox controls, shown here for the Date Taken text box, disables commands that are not currently available.

9.2.5USING TEXTBOX CONTROLS

So let’s add some interesting features to our text boxes. Most of the events for TextBox controls are inherited from the Control and TextBoxBase classes. Members

LABELS AND TEXT BOXES

287

specific to the TextBox class appear in .NET Table 9.3. Here we will look more closely at the KeyPress event and the TextChanged event.

The keyboard events inherited from the Control class are especially interesting, and consist of the KeyDown, KeyPress, and KeyUp events. These events are inherited from the Control class, and occur when a key on the keyboard is pushed down and released while the control has focus. The KeyDown event occurs when the key is first pressed. The KeyPress event activates while the key is held down and repeats while the key remains held down. The KeyUp event occurs when the key is released. These events can be used to fine-tune your interfaces as the user types on the keyboard.

.NET Table 9.3 TextBox class

The TextBox class represents a TextBoxBase control that displays a single font. This control is part of the System.Windows.Forms namespace, and inherits from the TextBoxBase control. Through its parent class, text boxes can support single or multiple lines, and interact with the clipboard to cut, copy, or paste text.

 

AcceptsReturn

Gets or sets whether the Enter key in a multiline

 

 

text box adds a new line of text or activates the

 

 

default button for the form.

 

CharacterCasing

Gets or sets how the control modifies the case

 

 

of entered characters. This can be used to

 

 

display all uppercase or lowercase letters in the

 

 

text box.

Public Properties

PasswordChar

Gets or sets the character used to mask the text

 

 

 

display in the control. When this property is set,

 

 

cutting or copying to the clipboard is disabled.

 

ScrollBars

Gets or sets which scrollbars should appear in a

 

 

multiline text box.

 

TextAlign

Gets or sets how displayed text is aligned within

 

 

the control.

 

 

 

Public Events

TextAlignChanged

Occurs when the TextAlign property has

 

changed.

 

 

 

 

 

We will look at the keyboard events in more detail in chapter 12, but let’s do a quick example here. Suppose we wanted the Caption property to only contain letters or numbers. No punctuation characters and no symbols. The KeyPress event receives keyboard characters as they are typed, and allows the event handler to handle or ignore them. The KeyPressEventArgs class is used with this event, and provides a KeyChar property to get the character pressed, and a Handled property to get or set whether the character has been handled. If Handled is set to true, then the control will not receive the character.

The obvious, albeit incorrect, way to implement such a handler would be as follows:

private void txtCaption_KeyPress(object sender, KeyPressEventArgs e)

{

288

CHAPTER 9 BASIC CONTROLS

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