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

SAVE A PHOTOS SETTINGS ONLY WHEN NECESSARY

 

Action

Result

 

 

 

12

Create a variable to hold

private bool _modifiedTxtNotes;

 

whether the notes value has

 

 

changed.

 

 

 

 

13

Modify the SetOriginals

protected void SetOriginals()

 

method to reset this variable.

{

 

 

. . .

 

 

if (photo != null)

 

 

{

 

 

. . .

 

 

_modifiedTxtNotes = false;

 

 

}

 

 

}

 

 

 

14

Add a TextChanged event

private void txtNotes_TextChanged

 

handler for the Notes text box

(object sender, System.EventArgs e)

 

to update this variable

{

 

if (txtNotes.Focused)

 

whenever the user changes

 

_modifiedTxtNotes = true;

 

the text.

}

 

 

 

15

Create a NewControlValues

protected bool NewControlValues()

 

method to determine if any of

{

 

photograph’s controls have

bool result =

 

((_origCaption != txtCaption.Text)

 

been modified.

 

|| (_origDateTaken != dtpDateTaken.Value)

 

 

|| (_origPhotographer !=

 

 

cmbxPhotographer.Text)

 

 

|| (_modifiedTxtNotes));

 

 

return result;

 

 

}

 

 

 

16

Add a variable to track whether

private bool _hasChanged = false;

 

any changes have been saved.

 

 

 

 

17

Modify the SaveSettings

protected override bool SaveSettings()

 

method to save the changes

{

 

made to the photograph.

if (NewControlValues())

 

{

 

 

 

Note: The _hasChanged field

// Save the photograph’s settings

 

is required to track whether

Photograph photo = _album[_index];

 

 

 

any photograph has been

if (photo != null)

 

modified, as opposed to the

{

 

current photo displayed.

photo.Caption = txtCaption.Text;

 

 

photo.DateTaken = dtpDateTaken.Value;

 

 

photo.Photographer

 

 

= cmbxPhotographer.Text;

 

 

photo.Notes = txtNotes.Text;

 

 

_hasChanged = true;

 

 

}

 

 

}

 

 

return true;

 

 

}

 

 

 

18

Add a HasChanged property so

public bool HasChanged

 

a caller can determine if any

{

 

photographs were saved.

get { return _hasChanged; }

 

}

 

 

 

 

 

IMAGE BUTTONS

397

With these changes in place, we can finally add the click handlers for our buttons.

HANDLE THE CLICK EVENT FOR THE NEXT AND PREV BUTTONS

 

Action

Result

 

 

 

19

Add a Click handler for the

private void btnNext_Click

 

Next button.

(object sender, System.EventArgs e)

 

 

{

 

 

SaveSettings();

 

 

if (_index < _album.Count - 1)

 

 

{

 

 

_index ++;

 

 

ResetSettings();

 

 

SetOriginals();

 

 

}

 

 

}

 

 

 

20

Add a Click handler for the

private void btnPrev_Click

 

Prev button.

(object sender, System.EventArgs e)

 

 

{

 

 

SaveSettings();

 

 

if (_index > 0)

 

 

{

 

 

_index --;

 

 

ResetSettings();

 

 

SetOriginals();

 

 

}

 

 

}

 

 

 

As we said at the start of all this, these buttons required more changes than you might initially expect. As a final change, we need to modify the Click handler for our Photo Properties menu to account for the new changes.

Set the version number of the MyPhotos application to 12.3.

UPDATE THE CLICK HANDLER TO DISPLAY THE DIALOG

 

Action

Result

 

 

 

21

Update the Click handler for

private void menuPhotoProp_Click

 

the menuPhotoProp control

(object sender, System.EventArgs e)

 

to use the HasChanged

{

 

if (_album.CurrentPhoto == null)

 

property.

 

return;

 

 

using (PhotoEditDlg dlg

 

 

= new PhotoEditDlg(_album))

 

 

{

 

 

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

 

 

{

 

 

_bAlbumChanged = dlg.HasChanged;

 

 

if (_bAlbumChanged)

 

 

{

 

 

// Redraw to pick up any changes

 

 

this.Invalidate();

 

 

}

 

 

}

 

 

}

 

 

}

 

 

 

398

CHAPTER 12 A .NET ASSORTMENT

Our buttons are now ready to go. Compile and run your code to make sure the buttons work as expected. Our next topic is the creation of bitmap objects for these buttons.

12.3.2DRAWING BITMAPS FOR OUR BUTTONS

The creation of graphics for a new product is typically left to the graphic designers and marketing folks. Still, you never know when you may feel the urge to draw a bitmap or icon yourself, so let’s look at how to create them from scratch. Fortunately, Microsoft provides a fairly extensive collection of bitmaps, cursors, and icons that can be used within our application. We’ll talk about where to find these in a moment.

A bitmap is created much like any other item in a project. We will begin by creating a bitmap object for our Next button. Later in this section we’ll create another bitmap for our Prev button.

 

CREATE THE NEXT BUTTON BITMAP

 

 

 

 

Action

Result

 

 

 

1

In the Solution Explorer

 

 

window, add a bitmap file

 

 

“NextButton.bmp” to the

 

 

MyPhotoAlbum project.

 

 

How-to

 

 

a. Right-click the MyPhoto-

 

 

Album project name.

 

 

b. Open the Add submenu.

 

 

c. Select Add New Item to

 

 

display the Add New

 

 

Item dialog box.

 

 

d. Click Resources from

 

 

the list of Categories

 

 

e. Click Bitmap File from

 

 

the available Templates.

 

 

f. Enter “NextButton.bmp”

 

 

as the name.

 

 

Alternately

 

 

The Add New Item dialog

 

 

can be displayed using the

 

 

keyboard shortcut

 

 

Ctrl+Shift+A.

 

 

 

 

2

Click the Open button to

The new file appears in Solution Explorer.

 

create the new file.

The Bitmap Editor appears in the main window and displays

 

 

 

 

the new bitmap with the default size, which is normally 48×48

 

 

pixels.

 

 

Note: The new file and Bitmap Editor are shown in

 

 

figure 12.3.

 

 

 

IMAGE BUTTONS

399

 

CREATE THE NEXT BUTTON BITMAP

(continued)

 

 

 

 

 

Action

 

Result

 

 

 

 

3

Modify the bitmap to have

 

 

 

size 18 ×18 pixels.

 

 

 

How-to

 

 

 

a. Right-click within the Bit-

 

 

 

map Editor window.

 

 

 

b. Select the Properties item

 

 

 

c. Set the Height and Width

 

 

 

items to 18.

 

 

 

 

Note: The Colors property here is used to select between

 

 

Monochrome, 16 Color, 256 Color, and True Color images.

 

 

For our purposes, the default of 16 Color is fine.

 

 

 

 

4

Edit the pixels for the

 

 

 

bitmap to create a right-

 

 

 

direction arrow.

 

 

 

How-to

 

 

 

Copy the graphic shown

 

 

 

here, or create your own

 

 

 

version of this arrow.

 

 

 

 

 

 

This completes our Next button. Figure 12.3 shows Visual Studio .NET with this button displayed. If you are feeling creative, the editor supports a wide range of drawing controls, in many ways similar to the Microsoft Paint application installed with the Windows operating systems. In the figure, the drawing controls are available in the bottom row of toolbar buttons, and the Colors window is shown on the left side of the window. If not shown, the Colors window is displayed by right-clicking within the Bitmap Editor window and selecting the Show Colors Window item.

A bitmap for the Prev button can be created in a similar manner as described in the previous table. An alternate method of creating this file is used in the following steps:

CREATE THE PREV BUTTON BITMAP

 

Action

Result

 

 

 

5

Make a copy of the NextButton.bmp file.

A new bitmap file called “Copy of

 

How-to

NextButton.bmp” is added to the project.

 

 

 

a. Right-click on the file and select Copy.

 

 

b. Right-click on the MyPhotoAlbum project

 

 

name and select Paste.

 

 

 

 

6

Rename this file to “PrevButton.bmp.”

 

 

 

 

400

CHAPTER 12 A .NET ASSORTMENT

 

CREATE THE PREV BUTTON BITMAP

(continued)

 

 

 

 

 

Action

 

Result

 

 

 

 

7

Flip the existing bitmap horizontally to create a

 

 

 

left-pointing arrow.

 

 

 

How-to

 

 

 

a. Double-click on the PrevButton.bmp file

 

 

 

name to display the Bitmap Editor window.

 

 

 

b. Right-click within the window to display its

 

 

 

popup menu.

 

 

 

c. Select the Flip Horizontal item.

 

 

 

 

 

 

Our two bitmaps are now ready for use. The next step is to assign these bitmaps to the buttons on our form.

Figure 12.3 The Bitmap Editor window displays both an actual size and a per-pixel view of the bitmap.

IMAGE BUTTONS

401

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