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

UPDATE THE CLICK HANDLER FOR THE PROPERTIES MENU (continued)

 

Action

Result

 

 

 

5

Display the properties dialog for

using (PhotoEditDlg dlg

 

the photo.

= new PhotoEditDlg(_album))

 

 

{

 

 

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

 

 

{

 

 

 

6

If any changes were made in the

// Save any changes made

 

dialog, save the entire album to

try

 

disk.

{

 

_album.Save(_album.FileName);

 

 

 

Note: As you’ll recall, we permit

}

 

multiple photographs to be mod-

catch (Exception)

 

{

 

ified in the dialog. As a result,

 

MessageBox.Show("Unable to save "

 

the entire album must be saved

+ "changes to photos in album.");

 

and reloaded into the control to

}

 

pick up any changes.

 

 

 

 

7

Reload the entire album into the

// Update the list with any new settings

 

control to pick up the new

LoadPhotoData(_album);

 

changes.

}

 

}

 

 

 

 

}

 

 

 

14.5.6UPDATING LABEL EDITING

Updating the label for our photographs again does not use any new constructs, so we will hurry through this code as well. As you’ll recall, the caption for each photograph is displayed as the item label. We should note that the menuEditLabel_Click handler does not require any changes, since this simply initiates the edit. The AfterLabelEdit event handler is where the new value is processed.

UPDATE THE AFTERLABELEDIT EVENT HANDLER

 

Action

Result

 

 

 

1

In the MainForm.cs code

private void listViewMain_AfterLabelEdit

 

window, modify the After-

(object sender, System.Windows.

 

LabelEdit event handler to call

Forms.LabelEditEventArgs e)

 

{

 

a new UpdatePhotoCaption

 

if (e.Label == null)

 

method to process an edit when

{

 

photographs are displayed.

// Edit cancelled by the user

 

 

e.CancelEdit = true;

 

 

return;

 

 

}

 

 

ListViewItem item =

 

 

listViewMain.Items[e.Item];

 

 

if (this._albumsShown)

 

 

e.CancelEdit = !UpdateAlbumName(e.Label,

 

 

item);

 

 

else

 

 

e.CancelEdit = !UpdatePhotoCaption(e.Label,

 

 

item);

 

 

}

 

 

 

ITEM ACTIVATION

481

UPDATE THE AFTERLABELEDIT EVENT HANDLER (continued)

 

Action

Result

 

 

 

2

Add the UpdatePhotoCaption

private bool UpdatePhotoCaption

 

method to the MainForm class.

(string caption, ListViewItem item)

 

 

{

 

 

 

3

Make sure the new caption is

if (caption.Length == 0 || !(item.Tag is int))

 

not empty.

{

 

 

MessageBox.Show("Invalid caption value.");

 

 

return false;

 

 

}

 

 

 

4

Determine the index for this

int index = (int)item.Tag;

 

photograph.

 

 

 

 

5

Set the photograph’s caption to

_album[index].Caption = caption;

 

the new value.

 

 

 

 

6

Save the album to store the new

try

 

value.

{

 

 

_album.Save(_album.FileName);

 

 

}

 

 

catch (Exception)

 

 

{

 

 

MessageBox.Show("Unable to save new "

 

 

+ "caption to album file.");

 

 

}

 

 

return true;

 

 

}

 

 

 

One further change we can make here is to alter the text displayed in the corresponding menu item. This will provide visual feedback to the user on which property they are actually changing, especially when the Details view is not displayed.

MODIFY THE TEXT DISPLAYED IN THE EDIT LABEL MENU

 

Action

Result

 

 

 

7

In the MainForm.cs [Design]

private void menuEdit_Popup

 

window, add a Popup event

(object sender, System.EventArgs e)

 

handler for the menuEditLabel

{

 

 

 

menu.

 

 

 

 

8

Enable the contained menus

menuEditLabel.Enabled

 

only if a single item is selected

= (listViewMain.SelectedItems.Count == 1);

 

in the view.

menuProperties.Enabled

 

= (listViewMain.SelectedItems.Count == 1);

 

 

 

 

 

9

Set the menu’s text to “Name”

if (this._albumsShown)

 

or “Caption” depending on

menuEditLabel.Text = "&Name";

 

which type of object is displayed

else

 

menuEditLabel.Text = "&Caption";

 

in the list.

 

}

 

 

 

482

CHAPTER 14 LIST VIEWS

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