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

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(344, 125); this.ControlBox = false;

this.Controls.AddRange(new System.Windows.Forms.Control[] { this.txtCaption, this.lblImage, this.label2,

this.label1,

this.btnCancel,

this.btnOK});

this.FormBorderStyle

=System.Windows.Forms.FormBorderStyle.FixedDialog; this.MaximizeBox = false;

this.MinimizeBox = false; this.Name = "CaptionDlg"; this.ShowInTaskbar = false; this.StartPosition

=System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "CaptionDlg";

this.ResumeLayout(false);

}

#endregion

Our dialog box is now ready from a display perspective. There is still the matter of making sure our main form can make use of it to edit a caption for an image. For this, we will need to add some properties to our CaptionDlg class.

8.3.4ADDING PROPERTIES TO THE CAPTIONDLG FORM

So far we have modified our data layer to understand photograph captions, and created a dialog to use for editing this caption. To integrate this dialog with the rest of our application, we need to ensure that the MainForm class can set the text to appear in the Label and TextBox controls of a CaptionDlg instance before the dialog is displayed, and retrieve the modified TextBox value after the dialog is finished. We must also ensure that the result of the dialog, whether the user pressed OK or Cancel, can be detected by the caller.

If we were programming in C++, we might create SetImage, SetCaption, and GetCaption methods to allow the lblImage and txtCaption values to be modified. In C#, the property construct provides a slightly more elegant solution.

ADD IMAGELABEL AND CAPTION PROPERTIES TO OUR DIALOG

 

Action

Result

 

 

 

1

Add an ImageLabel property

public string ImageLabel

 

to the CaptionDlg.cs source

{

 

window.

 

 

 

 

MODAL DIALOG BOXES

247

ADD IMAGELABEL AND CAPTION PROPERTIES TO OUR DIALOG (continued)

 

Action

Result

 

 

 

2

Implement set for this

set { lblImage.Text = value; }

 

property to modify the text for

}

 

the lblImage label.

Note: This code relies on the .NET Framework to dis-

 

 

play this value in the control and deal with an empty

 

 

or null string.

 

 

 

3

Add a Caption property.

public string Caption

 

 

{

 

 

 

4

Implement set for this

set { txtCaption.Text = value; }

 

property to modify the text for

 

 

the txtCaption text box.

 

 

 

 

5

Implement get to return this

get { return txtCaption.Text; }

 

text value.

}

 

 

 

These properties will allow the values in the dialog to be set and retrieved as appropriate. Our last task is to return the appropriate value based on which button the user selects.

We have already seen how the DialogResult enumeration encapsulates the possible values returned by the MessageBox.Show method. For Form objects, the ShowDialog method returns this enumeration for a similar purpose. Here, we would like this method to return DialogResult.OK if the user clicks the OK button or presses the Enter key, and DialogResult.Cancel if the user clicks the Cancel button or presses the Esc key.

The Form class handles the keyboard values, namely the Enter and Esc keys, directly via the AcceptButton and CancelButton properties. We will look at returning the proper value from the ShowDialog method in a moment.

ALLOW THE DIALOG TO BE CLOSED VIA THE KEYBOARD

 

Action

Result

 

 

 

6

From the CaptionDlg.cs

 

 

[Design] window, assign

 

 

the OK button as the

 

 

accept button for the form,

 

 

to be activated when the

 

 

user presses the Enter key.

 

 

How-to

 

 

a. Display the properties for

 

 

the form.

 

 

b. Click the down arrow to

 

 

the right of the Accept-

 

 

Button entry in the list.

 

 

c. Select btnOK.

 

 

 

 

248

CHAPTER 8 DIALOG BOXES

ALLOW THE DIALOG TO BE CLOSED VIA THE KEYBOARD (continued)

 

Action

Result

 

 

 

7

Similarly, set the

These settings assign each property to the selected button

 

CancelButton property to

object in the InitializeComponent method of the

 

the btnCancel button.

CaptionDlg.cs source file.

 

 

this.AcceptButton = this.btnOK;

 

 

this.CancelButton = this.btnCancel;

 

 

 

The Form now invokes the OK or Cancel button when the user presses the Enter or Esc key, respectively.

We also need to ensure that the ShowDialog method returns the proper result when one of these buttons is clicked or invoked via the keyboard. The Button class provides a DialogResult property for this purpose. When this property is set, clicking the corresponding button will automatically hide the parent form and return the selected result.3 We already set these properties to the appropriate values while creating the buttons, so the InitializeComponent method already defines these settings.

btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;

btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;

We will see how the settings interact with the ShowDialog method when we display the dialog from our MainForm class. This is our next topic.

8.3.5DISPLAYING THE DIALOG IN THE MAINFORM CLASS

We can now turn to our MainForm class in order to invoke the dialog and edit a photo’s caption. We do this via our menu bar, of course. Since we are editing an aspect of the photo, we will place a Caption menu item under the Edit menu. This section defines our new menu and creates a Click handler for this menu to display the CaptionDlg form to the user.

We will begin by adding a new menu item to our main form.

 

ADD A CAPTION ITEM TO THE EDIT MENU

 

 

 

 

 

Action

 

Result

 

 

 

 

1

Click the Edit menu in the

 

 

 

MainForm.cs [Design]

 

 

 

window.

 

 

 

 

 

 

2

Add a menu separator after

 

 

 

the Remove menu item.

 

 

 

 

 

 

 

 

 

 

3 The term “hide” here is intentional. Pursuant to our earlier discussion on the equivalence of Close and Dispose, modal dialog boxes must be disposed of manually to allow their members to be accessed after ShowDialog returns. As a result, the framework only hides our dialog by calling the Hide method when the OK or Cancel button is clicked.

MODAL DIALOG BOXES

249

 

 

 

ADD A CAPTION ITEM TO THE EDIT MENU

(continued)

 

 

 

 

 

 

 

 

 

Action

 

Result

 

 

 

 

 

 

 

3

Add a “&Caption…” menu

 

 

 

under the separator.

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

Property

Value

 

 

 

 

(Name)

menuCaption

 

 

 

 

Enabled

False

 

 

 

 

Text

&Caption...

 

 

 

 

 

 

 

 

 

We set the Enabled property for the menu to false since an image will not be shown when the application starts.

To enable this menu, we could use the existing OnPaint method of our form. A simpler approach is to add a Popup event handler for the parent Edit menu, and enable or disable the Caption menu as required just before it displays.

ENABLE THE CAPTION MENU IN A POPUP HANDLER

 

Action

Result

 

 

 

4

Add a Popup event handler

public void menuEdit_Popup

 

for the Edit menu.

(object sender, EventArgs e)

 

How-to

{

 

 

 

Double-click the Popup

 

 

item in the list of events for

 

 

the Edit menu.

 

 

 

 

5

Set the Enabled property

menuCaption.Enabled = (_album.Count > 0);

 

for the Caption menu

}

 

based on whether an

 

 

image is currently

 

 

displayed.

 

 

 

 

Our Caption menu is enabled whenever photographs are available and therefore displayed, and disabled when the album is empty.

The final task in this section is to implement a Click handler for our menu. This handler will display our CaptionDlg form and modify the caption as required. This task continues the previous steps and pulls together all the changes we have made in this section.

250

CHAPTER 8 DIALOG BOXES

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