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

Set the version number of the MyPhotos application to 6.6.

IMPLEMENT HANDLER FOR SAVE AS MENU

 

Action

Result

 

 

 

1

Add a Click handler to the

protected void menuSaveAs_Click

 

Save As menu.

(object sender, System.EventArgs e)

 

 

{

 

 

 

2

Create a SaveFileDialog

SaveFileDialog dlg = new SaveFileDialog();

 

instance and initialize the

dlg.Title = "Save Album";

 

properties as discussed.

 

dlg.DefaultExt = "abm";

 

 

 

Note: In the Filter property

dlg.Filter = "Album files (*.abm)|*.abm|"

 

setting, we permit all files to

+ "All files|*.*";

 

dlg.InitialDirectory = PhotoAlbum.DefaultDir;

 

be shown, even though only

 

dlg.RestoreDirectory = true;

 

the abm extension is a recog-

 

 

nized album file. This is not

 

 

necessary, but a nice conve-

 

 

nience to allow the user to see

 

 

all files in a directory.

 

 

 

 

3

Once a user selects a file,

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

 

record the album name and

{

 

save the current album using

// Record the new album name

 

_album.FileName = dlg.FileName;

 

this name.

 

 

 

 

// Use Save handler to store the album

 

 

menuSave_Click(sender, e);

 

 

// Update title bar to include new name

 

 

SetTitleBar();

 

 

}

 

 

 

4

Dispose of nonmemory

dlg.Dispose();

 

resources used by the dialog.

}

 

 

 

You will note that our code for the menuSaveAs_Click handler is reminiscent of our previous use of the OpenFileDialog class. The album is saved only if the user clicks the OK button. The yet-to-be-implemented Save menu handler actually saves the file.

Also note the use of the RestoreDirectory property. We set this to true so that the current directory setting for the application is restored after the dialog exits. By default, this property is set to false, and the current directory for the application is modified to match the final directory in the dialog. You may recall that we set the InitialDirectory setting for our menuAdd_Click handler to the current directory via the CurrentDirectory property of the Environment class. Since we have different menus interacting with the file system in different ways, we ensure that the initial directory seen for each menu makes some amount of sense.

6.6.3SAVING AN EXISTING ALBUM

We come at last to the Save menu handler. Here we need to select an album file name if one does not already exist, and save the actual data associated with the album.

188

CHAPTER 6 COMMON FILE DIALOGS

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