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

Before we go on, we should point out that our OnClosing override can be written more succinctly by taking advantage of the boolean value returned by our close album method.

protected override void OnClosing(CancelEventArgs ce)

{

ce.Cancel = (!this.CloseCurrentAlbum());

base.OnClosing(ce);

}

Now that we know all about closing a dialog box, let’s see how to create one of our own.

8.3MODAL DIALOG BOXES

In earlier chapters, we added controls such as a Button, PictureBox, and StatusBar to our main form, and displayed and managed these objects within the Form class on behalf of our application. In this section we will see how a dialog box can be created and displayed to further our understanding of the Form object.

As a way to introduce this concept, we will add the ability to assign a caption to an image. This caption will be a text string supplied by the user. The dialog box shown in figure 8.5 will allow the user to modify this value. The base file name of the image will be used as the default caption.

Figure 8.5

Our dialog box will contain three text labels, a text box, and two buttons.

In order to support this dialog, we will need to modify three aspects of our application:

1Data layer. Our Photograph class must support a caption on an image, and our PhotoAlbum class must store and retrieve these captions when saving and opening files.

2Presentation layer. We need a class to display our form as a dialog box. We will call this class CaptionDlg. This class must provide the interface and a means for returning a new caption value set by the user.

3Application layer. Our MainForm class must provide access to the new interface, and the link between the interface layer in our CaptionDlg class and the data layer in our MyPhotoAlbum library.

We will address each of these layers separately in order to create our new dialog.

MODAL DIALOG BOXES

237

8.3.1ADDING CAPTIONS TO PHOTOS

Let’s begin with the data layer. In this section we will support captions on photographs, and in the next section store and retrieve captions in our photo album files. In the Photograph class, we need to track the caption value, and allow external classes to set and get this value. These changes are detailed by the following steps.

Set the version number of the MyPhotoAlbum library to 8.3.

ADD A CAPTION TO THE PHOTOGRAPH CLASS

 

Action

Result

 

 

 

1

In the Photograph.cs file,

private string _fileName;

 

add a private _caption

private Bitmap _bitmap;

 

field to hold the caption for

private string _caption;

 

 

 

the object.

 

 

 

 

2

Initialize the caption to the

using System.IO;

 

base name of the file in the

. . .

 

constructor.

public Photograph(string fileName)

 

{

 

How-to

 

_fileName = fileName;

 

a. Add a using System.IO

_bitmap = null;

 

statement at the top of

_caption = Path.

 

GetFileNameWithoutExtension(_fileName);

 

the file.

 

}

 

b. Use the Path class to

 

 

retrieve the base file

 

 

name.

 

 

 

 

3

Add a Caption property.

public string Caption

 

 

{

 

 

 

4

Implement the get

get { return _caption; }

 

accessor to return the

 

 

current caption.

 

 

 

 

5

Implement the set

set

 

accessor to revert to the

{

 

default on null, and

if (value == null || value.Length == 0)

 

{

 

otherwise use the given

 

_caption = Path.

 

value.

GetFileNameWithoutExtension(_fileName);

 

 

}

 

 

else

 

 

{

 

 

_caption = value;

 

 

}

 

 

}

 

 

}

 

 

Note: The value keyword is used as a string object

 

 

here since the containing property is of type string.

 

 

 

We now have the ability to set captions for individual photographs. This will not do us much good unless our album class preserves these captions in the album file. For this we need to modify the Open and Save methods.

238

CHAPTER 8 DIALOG BOXES

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