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

.NET Table 14.7 LabelEditEventArgs class

The LabelEditEventArgs class represents the event arguments received by BeforeLabelEdit and AfterLabelEdit event handlers for the ListView class. This class is part of the System.Windows.Forms namespace, and inherits from the System.EventArgs class.

 

CancelEdit

Gets or sets whether the edit operation should be cancelled.

 

 

This property can be set both before and after the item is

Public

 

edited.

 

 

Properties

Item

Gets the zero-based index into the list view’s Items collection

 

 

of the ListViewItem to be edited.

 

Label

Gets the new text to assign to the label of the indicated item.

 

 

 

Since we are not in a production environment, we will take the easy way out and only handle the AfterLabelEdit event. This means a user may edit an album only to find that he or she cannot save his changes, which is not the best interface from a usability perspective.

The code changes required are given in the following steps:

 

 

 

 

 

INITIATE LABEL EDITING

 

 

 

 

 

 

 

 

 

 

Action

Result

 

 

 

 

 

 

1

In the MainForm.cs [Design]

Item labels in the list view may now be edited.

 

window, set the LabelEdit

 

 

property of the ListView control

 

 

to true.

 

 

 

 

 

 

 

 

 

 

2

Add a Name menu to the top of

 

 

the Edit menu.

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

Property

 

Value

 

 

 

(Name)

 

menuEditLabel

 

 

 

Text

 

&Name

 

 

 

 

 

 

 

 

3

Add a Click event handler for

private void menuEditLabel_Click

 

this menu.

 

 

 

(object sender, System.EventArgs e)

 

 

 

 

 

 

{

 

 

 

 

 

 

4

Within this handler, if an item is

if (listViewMain.SelectedItems.Count == 1)

 

selected, edit the item.

listViewMain.SelectedItems[0].BeginEdit();

 

 

 

 

 

 

}

 

 

 

 

 

 

Note: This code only edits the label if a single item

 

 

 

 

 

 

is selected. While we do not permit multiple items

 

 

 

 

 

 

to be selected in our ListView control, this code

 

 

 

 

 

 

establishes an appropriate behavior in case such

 

 

 

 

 

 

selection is ever permitted in the future.

 

 

 

 

 

 

5

Add a KeyDown event handler for

private void listViewMain_KeyDown

 

the ListView control.

(object sender, System.Windows.

 

 

 

 

 

 

Forms.KeyEventArgs e)

 

 

 

 

 

 

{

 

 

 

 

 

 

 

SELECTION AND EDITING

469

INITIATE LABEL EDITING (continued)

 

Action

Result

 

 

 

6

If the F2 key is pressed and an

if (e.KeyCode == Keys.F2)

 

item is selected, edit the item.

{

 

 

if (listViewMain.SelectedItems.Count == 1)

 

 

{

 

 

listViewMain.SelectedItems[0].

 

 

BeginEdit();

 

 

e.Handled = true;

 

 

}

 

 

}

 

 

}

 

 

 

That’s all it takes to begin an edit. The actual work of interacting with the user is handled by the framework. When the user is finished, we can pick up the result in an AfterLabelEdit event handler. There is also a BeforeLabelEdit event that is useful for selectively permitting an edit or altering an item before the edit begins. For our purposes, the AfterLabelEdit event will suffice.

 

 

PROCESS A LABEL EDIT

 

 

 

 

Action

Result

 

 

 

7

Add an AfterLabelEdit

private void listViewMain_AfterLabelEdit

 

event handler for the

(object sender, System.Windows.

 

ListView control.

Forms.LabelEditEventArgs e)

 

{

 

 

 

 

 

8

If the user cancelled the edit,

if (e.Label == null)

 

then we are finished.

{

 

Note: For example, if the

// Edit cancelled by the user

 

e.CancelEdit = true;

 

user presses the Esc key dur-

return;

 

ing editing, this handler is

}

 

 

 

invoked with a null label.

 

 

 

 

9

In this handler, locate the item

ListViewItem item = listViewMain.Items[e.Item];

 

to be edited.

 

 

 

 

10

Update the album name, and

if (UpdateAlbumName(e.Label, item) == false)

 

cancel the edit if an error

e.CancelEdit = true;

 

occurs.

}

 

 

 

Note: Once again we sepa-

 

 

rate the logic to operate on

 

 

our album into a separate

 

 

method.

 

 

 

 

470

CHAPTER 14 LIST VIEWS

PROCESS A LABEL EDIT (continued)

 

Action

Result

 

 

 

11

Add the UpdateAlbumName

private bool UpdateAlbumName

 

method to update the title of

(string newName, ListViewItem item)

 

the album.

{

 

string fileName = item.Tag as string;

 

 

 

How-to

string newFileName

 

= RenameFile(fileName, newName, ".abm");

 

a. Retrieve the file name from

 

if (newFileName == null)

 

the Tag property for the

{

 

item.

MessageBox.Show(

 

b. Rename the file using a pri-

"Unable to rename album to this name.");

 

return false;

 

vate method that returns

}

 

the new name.

// Update Tag property

 

c. Inform the user if the file

 

item.Tag = newFileName;

 

could not be renamed.

return true;

 

d. Otherwise, update the Tag

}

 

 

 

property with the new

 

 

name.

 

 

 

 

12

Implement the RenameFile

private string RenameFile

 

method to construct the new

(string origFile, string newBase, string ext)

 

name for the file.

{

 

string fileName = Path.

 

 

 

How-to

GetDirectoryName(origFile) + "\\" + newBase;

 

string newFile = Path.ChangeExtension(fileName,

 

a. Use the GetDirecto-

 

ext);

 

ryName method to retrieve

 

 

the directory for the file.

 

 

b. Use the ChangeExtension

 

 

method to ensure the file

 

 

has the correct extension.

 

 

 

 

13

Rename the file using the

try

 

Move method in the File

{

 

class.

File.Move(origFile, newFile);

 

return newFile;

 

 

 

 

}

 

 

 

14

Return null if an error

catch (Exception)

 

occurs.

{

 

 

// An error occurred

 

 

return null;

 

 

}

 

 

}

 

 

 

This code uses some methods from the Path and File classes to manipulate the file name strings and rename the album file. Our application now supports displaying album properties and editing of album labels. The next topic of discussion is item activation.

SELECTION AND EDITING

471

14.5ITEM ACTIVATION

As you might expect, item activation is the means by which an item is displayed or otherwise activated by the control. Normally, activation is just a fancy way to say double-click. In our ListBox class in chapter 10, we activated an item in the list by handling the DoubleClick event and displaying the properties dialog associated with the item. Such behavior is activation.

The reason for the fancy term is that the ListView class allows activation other than a double-click to be supported. The Activation property determines the type of activation supported, based on the ItemActivation enumeration. The possible values for this enumeration are shown in .NET Table 14.8. Note that the OneClick style is similar to an HTML link in a Web browser. In our program, we will stick with the standard activation.

.NET Table 14.8 ItemActivation enumeration

The ItemActivation enumeration specifies the type of activation supported by a control. This enumeration is part of the System.Windows.Forms namespace.

 

OneClick

A single click activates an item. The cursor appears as a

 

 

hand pointer, and the item text changes color as the mouse

Enumeration

 

pointer passes over the item.

 

 

Values

Standard

A double-click activates an item.

 

TwoClick

A double-click activates an item, plus the item text changes

 

 

color as the mouse pointer passes over the item.

 

 

 

Regardless of how items are activated, an ItemActivate event occurs whenever an item is activated. The event handler for this event receives a standard System.EventArgs parameter, so the activated item is obtained from the SelectedItems collection.

The activation behavior for our MyAlbumExplorer application will display the Photographs in the selected album. This is a rather complicated change, since the columns and list item behavior must now accommodate the display of both albums and photos here. The fact that we were careful to separate much of the album logic into individual methods along the way will help us keep our code straight. Figure 14.6 shows our application with photographs displayed in the ListView control. These photographs are sorted by the date each photo was taken. The icon used here might not be your first choice for a photograph icon, but it will suffice for our purposes. If you find another icon you prefer, or are feeling creative, you can use an alternate icon in your application.

472

CHAPTER 14 LIST VIEWS

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