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

.NET Table 6.1 OpenFileDialog class

The OpenFileDialog class represents a common file dialog box for loading one or more files from disk, and is part of the System.Windows.Forms namespace. This class inherits from the FileDialog class, and is the standard class for opening existing files. See .NET Table 1.2 on page 24 for a list of members inherited from the FileDialog class.

 

Multiselect

Gets or sets whether the user can select

 

 

multiple files in the dialog. The FileNames

 

 

property inherited from the FileDialog class

 

 

should be used to retrieve the selected files.

Public Properties

ShowReadOnly

Gets or sets whether the dialog should contain

 

 

 

a read-only check box. Defaults to false.

 

ReadOnlyChecked

Gets or sets whether the read only checkbox is

 

 

checked. Defaults to false.

 

 

 

Public Methods

OpenFile

Returns a Stream with read-only access for the

 

 

file specified by the FileName property.

 

 

 

The steps to implement a Click event handler for the Add menu are shown in the following table.

Set the version number of the MyPhotos application to 6.2.

 

 

IMPLEMENT ADD HANDLER

 

 

 

 

 

Action

 

Result

 

 

 

 

1

Open the Windows Forms

 

As we have seen before, a graphic of the current layout for

 

Designer window for the

 

this form is displayed.

 

MainForm.cs file.

 

 

 

 

 

 

2

Add a Click event handler for

 

A new menuAdd_Click method is added to and displayed in

 

the Add item under the Edit

 

the MainForm.cs source file.

 

menu.

 

The line to add the handler is created by Visual Studio in the

 

 

 

 

How-to

 

InitializeComponent method automatically:

 

Double-click on the menu

 

menuAdd.Click += new

 

item.

 

 

 

EventHandler (this.menuAdd_Click);

 

 

 

 

3

Remove the

 

Note: This code opens a single file and arranges to dis-

 

menuLoad_Click handler and

 

play it in the window. Here, we just want to add the file

 

copy its code into the

 

to the album, so some changes are required. The code

 

menuAdd_Click handler.

 

in the subsequent steps is based on the Load handler,

 

 

 

although there are some differences. In particular, we

 

 

 

do not handle any exceptions that might occur. This is

 

 

 

done intentionally so that we can discuss the handling

 

 

 

of such exceptions in chapter 7.

 

 

 

 

MULTIPLE FILE SELECTION

167

IMPLEMENT ADD HANDLER (continued)

 

Action

Result

 

 

 

4

Initialize an OpenFileDialog

protected void menuAdd_Click

 

instance to allow multiple

(object sender, System.EventArgs e)

 

selections of various image

{

 

OpenFileDialog dlg = new OpenFileDialog();

 

file types.

 

 

 

How-to

dlg.Title = "Add Photos";

 

dlg.Multiselect = true;

 

Use the Multiselect

 

dlg.Filter

 

property to allow multiple files

= "Image Files (JPEG, GIF, BMP, etc.)|"

 

to be selected.

+ "*.jpg;*.jpeg;*.gif;*.bmp;"

 

Note: The Filter setting

+ "*.tif;*.tiff;*.png|"

 

+ "JPEG files (*.jpg;*.jpeg)|*.jpg;*.jpeg|"

 

here includes most of the

+ "GIF files (*.gif)|*.gif|"

 

common formats users are

+ "BMP files (*.bmp)|*.bmp|"

 

+ "TIFF files (*.tif;*.tiff)|*.tif;*.tiff|"

 

likely to see. All of these for-

 

+ "PNG files (*.png)|*.png|"

 

mats are supported by the

+ "All files (*.*)|*.*";

 

Bitmap class.

dlg.InitialDirectory

 

 

= Environment.CurrentDirectory;

 

 

 

5

Invoke the dialog and process

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

 

an OK response.

{

 

 

 

6

Extract the array of files

string[] files = dlg.FileNames;

 

selected by the user.

 

 

 

 

7

Turn off the status bar panels

statusBar1.ShowPanels = false;

 

while the images are loading.

statusBar1.Text

 

 

= String.Format("Loading {0} Files",

 

 

files.Length);

 

 

 

8

Iterate through the array of

int index = 0;

 

selected files.

foreach (string s in files)

 

 

{

 

 

 

9

Add each image to the album

Photograph photo = new Photograph(s);

 

if it is not already present.

// Add the file (if not already present)

 

How-to

 

index = _album.IndexOf(photo);

 

Use the IndexOf method to

if (index < 0)

 

see if the photo is already in

{

 

index = _album.Add(photo);

 

the album.

 

_bAlbumChanged = true;

 

 

}

 

 

}

 

 

Note: The IndexOf method relies on the Equals override

 

 

we implemented in chapter 5.

 

 

 

10

Dispose of the nonmemory

dlg.Dispose();

 

resources used by the dialog.

 

 

 

 

11

Invalidate the main window to

this.Invalidate();

 

display the new settings.

}

 

 

}

 

 

 

In the code, note how the Multiselect property is used to permit multiple file selections. This property is one of the few OpenFileDialog members not inherited from the FileDialog class.

168

CHAPTER 6 COMMON FILE DIALOGS

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