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

The new menu structure for the MyPhotos application

Menu Bar

Menu Item

Shortcut

Description

 

 

 

 

 

File

Ctrl+N

Create a new photo album (the existing album is saved if

 

 

 

necessary).

 

Open

Ctrl+O

Open an existing photo album file.

File

Save

Ctrl+S

Save the current album.

 

Save As

 

Save the current album in a new file.

 

Exit

 

Exit the application. It should offer to save the current album

 

 

 

if any changes have been made.

 

 

 

 

Edit

Add

Ctrl+A

Add one or more photos to the album.

Remove

Ctrl+R

Remove the displayed photo from the album.

 

 

 

 

 

 

Image

 

We will leave this menu as already implemented. This

 

 

 

contains a submenu indicating how the image should be

View

 

 

displayed.

 

 

 

 

Next

Ctrl+Shift+N

Display the next image in the album, if any.

 

Previous

Ctrl+Shift+P

Display the previous image in the album, if any.

 

 

 

 

It is tempting to use our own terminology and establish our own conventions here. For example, why not use an “Album” main menu instead of the File menu, or have Ctrl+N as the shortcut for View-Next rather than File-New. The short answer: don’t do it. Computer users appreciate familiarity and resist change (so do most consumers and small children, but I digress). The File menu is standard in most Windows applications, and Ctrl+N is used for creating a new object (be it a document in Microsoft Word, or an image in Adobe PhotoDeluxe). Unless you are intending your application to be somewhat contrary, use existing standards where possible.

So even though Ctrl+N and Ctrl+P would make nice shortcuts for Next and Previous, we will stick with Ctrl+N for New and save Ctrl+P for when we add printing in part 3 of the book. We have already looked at menus in chapter 3, so this section will remove the existing Load menu and add our new menu structure.

Set the version number of the MyPhotos application to 6.1.

REMOVE THE LOAD MENU

 

Action

Results

 

 

 

1

In the MainForm.cs [Designer]

The menu and its properties are removed from the

 

window, remove the Load menu

program.

 

from the File menu.

Note: The menuLoad_Click method remains in

 

How-to

 

the source file. We will make use of this code

 

a. Right-click the menu item.

when we handle the Add menu later in this chapter.

 

b. Select Delete.

 

 

Alternately

 

 

Click the menu item and press the

 

 

Delete key.

 

 

 

 

DESIGN ISSUES

163

The creation of our menu requires the procedures we saw in chapter 3. The following table creates and positions the new menu items required in our application. The subsequent sections will look at the required event handlers for these menu items.

CREATE OUR NEW MENU

 

 

 

 

 

Action

 

 

 

 

Result

 

 

 

 

 

 

 

 

 

 

2

Create the new File menu structure, as shown in the

The new settings appear in the

 

following table.

 

 

 

 

 

 

 

window and are reflected in the

 

 

 

 

 

Settings

 

 

 

 

MainForm.cs file.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MenuItem

 

Property

 

Value

 

 

 

New

 

(Name)

 

menuNew

 

 

 

 

 

 

Shortcut

 

CtrlN

 

 

 

 

 

 

Text

 

&New

 

 

 

Open

 

(Name)

 

menuOpen

 

 

 

 

 

 

Shortcut

 

CtrlO

 

 

 

 

 

 

Text

 

&Open…

 

 

 

separator

 

 

 

 

 

 

 

 

 

 

Save

 

(Name)

 

menuSave

 

 

 

 

 

 

Shortcut

 

CtrlS

 

 

 

 

 

 

Text

 

&Save

 

 

 

Save As

 

(Name)

 

menuSaveAs

 

 

 

 

 

 

Text

 

Save &As...

 

 

 

separator

 

 

 

 

 

 

 

 

 

 

Exit

 

as before

 

 

 

 

 

 

Note: Recall that a menu separator is added by creat-

 

 

ing a MenuItem with the Text property set to a single

 

 

dash ‘-‘.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3

Define a new Edit menu between the existing File and

Note: This space intention-

 

View menus.

 

 

 

 

 

 

 

ally left blank.

 

How to

 

 

 

 

 

 

 

 

 

a. Click the “Type Here” entry on the menu bar, to the

 

 

right of the existing View menu.

 

 

b. Enter the name “&Edit” in this space and press

 

 

Enter.

 

 

 

 

 

 

 

 

 

c. Using the mouse, click on the new Edit menu and

 

 

drag it left to appear between the File and View

 

 

menus.

 

 

 

 

 

 

 

 

 

 

 

 

 

Settings

 

 

 

 

 

 

Property

 

Value

 

 

 

 

 

(Name)

 

menuEdit

 

 

 

 

 

 

Text

 

&Edit

 

 

Note: If you are not using Visual Studio, create the new

 

 

menu manually, and set the Index property for each of

 

 

the File, Edit, and View menus to 0, 1, and 2 respectively.

 

 

 

 

 

 

 

 

 

 

 

 

 

164

CHAPTER 6 COMMON FILE DIALOGS

CREATE OUR NEW MENU (continued)

 

 

 

 

 

Action

 

 

 

Result

 

 

 

 

 

 

 

 

 

 

 

4

Add the dropdown menu for the Edit menu.

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

MenuItem

Property

Value

 

 

 

 

Add

(Name)

menuAdd

 

 

 

 

 

 

Shortcut

CtrlA

 

 

 

 

 

 

Text

&Add

 

 

 

 

Remove

(Name)

menuRemove

 

 

 

 

 

 

Shortcut

CtrlR

 

 

 

 

 

 

Text

&Remove

 

 

 

 

 

 

 

 

 

 

 

 

5

Create the View menu structure:

 

 

 

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MenuItem

 

Property

 

Value

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Image

 

as before

 

 

 

 

 

 

 

 

separator

 

 

 

 

 

 

 

 

 

 

Next

 

(Name)

 

menuNext

 

 

 

 

 

 

Shortcut

 

CtrlShiftN

 

 

 

 

 

 

Text

 

&Next

 

 

 

 

Previous

 

(Name)

 

menuPrevious

 

 

 

 

 

 

Shortcut

 

CtrlShiftP

 

 

 

 

 

 

Text

 

&Previous

 

 

 

 

 

 

 

 

 

 

 

 

The source code generated here does not use anything we have not already seen and discussed in chapter 3, so we will move right along and start processing our new menus.

6.1.2ADDING CLASS VARIABLES

Before we can implement any event handlers for our menus, and in particular before

we can open and save album files, we must have a PhotoAlbum at our disposal in the MainForm class. In chapter 5 we added a reference to the MyPhotoAlbum library in the MyPhotos project, so this library is already available to our application.

Here we will add some protected variables to hold the displayed album and whether the user has made any changes to this album.

 

 

CREATE SOME CLASS VARIABLES

 

 

 

 

 

Action

 

Result

 

 

 

 

1

At the top of the

 

using Manning.MyPhotoAlbum;

 

MainForm.cs file, indicate

 

Note: Since we already reference the library (remember,

 

that we are using the

 

 

 

no header files required), we could write the fully quali-

 

MyPhotoAlbum

 

 

 

fied name every time we reference an object from the

 

namespace.

 

 

 

library. Typing Manning.MyPhotoAlbum.PhotoAlbum

 

 

 

 

 

 

repeatedly is not my idea of fun, so adding a using direc-

 

 

 

tive here makes sense.

 

 

 

 

DESIGN ISSUES

165

CREATE SOME CLASS VARIABLES (continued)

 

Action

Result

 

 

 

2

Within the MainForm class,

protected PhotoAlbum _album;

 

add a protected album

 

 

variable _album.

 

 

 

 

3

Add a protected boolean

protected bool _bAlbumChanged = false;

 

called _bAlbumChanged to

Note: This will be useful when deciding whether to save

 

track when an album is

 

an existing album before loading a new one or closing the

 

modified.

 

application. If no changes have occurred, then we will

 

 

 

 

know to not save the album.

 

 

 

4

Create an empty album at

public MainForm()

 

the end of the MainForm

{

 

constructor.

. . .

 

_album = new PhotoAlbum();

 

 

 

 

}

 

 

 

These variables are required to implement all of our new menu items in this chapter. With these in place, it is time to do just that.

6.2MULTIPLE FILE SELECTION

Now that we have an album in our MainForm class, albeit an empty one, we can add photos to it. In previous chapters, we allowed the user to read in a single photo, first using a button and later with a menu item. In our new structure, this has been replaced by the ability to add multiple photos to the album or remove the current photo from the album. Since this code builds on our original Load handler, it is a good place to begin.

As you would expect, we will provide Click handlers for both the Add and Remove items. The Add menu should allow one or more photos to be selected and added to the album, while the Remove menu should delete the currently displayed photo from the album. The Add menu will use the Multiselect property of the OpenFileDialog class, and is where our catchy section title comes from.

6.2.1ADDING IMAGES TO AN ALBUM

In chapter 3, the Click handler for the Load menu permitted a single file to be selected using the OpenFileDialog class. This made sense when only a single image was managed by the application. In this chapter, the idea of an album permits multiple images to be present at the same time. As a result, our user should also be able to load multiple images at once. This is again done using the OpenFileDialog class, so the code for this handler will be similar to the Click event handler for the

Load menu from chapter 3. The Multiselect property is provided by the OpenFileDialog class to indicate whether multiple files can be selected in the dialog. This and other members specific to this class are summarized in .NET Table 6.1.

166

CHAPTER 6 COMMON FILE DIALOGS

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