• No results found

Common file dialogs

6.1 Design issues 162 6.2 Multiple file selection 166 6.3 Paint events 169

6.4 Context menus revisited 173

6.5 Files and paths 175 6.6 Save file dialogs 181 6.7 Open file dialogs 189 6.8 Recap 193

In the previous chapter we created the Photograph and PhotoAlbum classes as a

way to encapsulate a photographic image and a collection of photographs. In this chapter we make use of these classes in our application to display a photo album to the user. We will stick with our model of one photo at a time, but allow a user to move forward and backward within the album. This will permit us to focus on inte- grating the library without too many changes to the user interface.

In future chapters, we will expand our class library with additional functionality such as storing the date a photograph was taken, or the name of a photographer. Add- ing such features to a photo album will take some work by the user, which we would not want to throw away when the program exits. As a result, before any of these addi- tional features really make sense, we need to store our photo album on the disk so it can be used again (and again and again).

How will we store our album on disk? In a file, of course, to match the chapter title. Specifically, this chapter will show how to perform the following tasks:

Use the PhotoAlbum and Photograph classes to display, navigate, and man-

age a set of photographs in the MyPhotos application.

Save the current album using the SaveFileDialog class.

Open a previously saved album using the OpenFileDialog class.

Figure 6.1 shows how our application will look by the end of this chapter, with the new File menu displayed. Note that we have also added an Edit menu and a new sta- tus bar panel showing the current position within the album. In the figure, the third of a total of four photographs in the album is shown.

6.1

DESIGN

ISSUES

The changes planned for this chapter require that we rewrite our menu bar. Before we plunge ahead, let’s do some brief design work to lay out this new main menu. It is always a good idea to sketch your graphical elements up front. You can even do this on paper. The point is to have in mind the graphical interface you wish to implement before you start writing code. While it is always possible to move menus and other objects around, it can also waste a lot of time. This is especially true if the application has to be approved by a manager, the customer, or anyone else. Doing a quick sketch on paper creates a basis for discussion and allows initial thoughts and ideas to be aired before a more formal design document or any code is written.

6.1.1 CHANGINGTHEMENUBAR

While we are not doing a formal design here, let’s at least write down the new menu structure we will implement. As shown in the following table, in addition to the File menu changes, we will also add an Edit menu to our application.

Figure 6.1

In this chapter we implement a more traditional File menu structure for our application.

DESIGNISSUES 163 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 appli- cations, 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 applica- tion to be somewhat contrary, use existing standards where possible.

So even though Ctrl+N and Ctrl+P would make nice shortcuts for Next and Pre- vious, 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.

The new menu structure for the MyPhotos application Menu Bar Menu Item Shortcut Description

File

File Ctrl+N Create a new photo album (the existing album is saved if necessary).

Open Ctrl+O Open an existing photo album 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.

View

Image We will leave this menu as already implemented. This contains a submenu indicating how the image should be 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.

REMOVETHE LOADMENU

Action Results

1 In the MainForm.cs [Designer] window, remove the Load menu from the File menu.

Alternately

Click the menu item and press the Delete key.

The menu and its properties are removed from the program.

Note: The menuLoad_Click method remains in the source file. We will make use of this code when we handle the Add menu later in this chapter. How-to

a. Right-click the menu item. b. Select Delete.

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 sub- sequent sections will look at the required event handlers for these menu items.

CREATEOURNEWMENU

Action Result

2 Create the new File menu structure, as shown in the following table.

Note: Recall that a menu separator is added by creat- ing a MenuItem with the Text property set to a single dash ‘-‘.

The new settings appear in the window and are reflected in the MainForm.cs file.

3 Define a new Edit menu between the existing File and View menus.

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.

Note: This space intention- ally left blank.

Settings

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

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