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

.NET Table 15.5 NodeLabelEditEventArgs class

The NodeLabelEditEventArgs class represents the event data associated with the BeforeLabelEdit and AfterLabelEdit events in the TreeView 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 node is edited.

Public Properties

Label

Gets the new text to assign to the label of the

 

 

 

indicated node.

 

Node

Gets the TreeNode object being edited.

 

 

 

In our application, we will permit nodes to be edited using the menuEditLabel menu item, or by pressing the F2 key when a tree node is selected and the tree view has the focus. The following table details the steps required for this change:

SUPPORT EDITING OF TREE NODE LABELS

 

Action

Result

 

 

 

1

Set the LabelEdit property for

Tree node labels may now be edited.

 

the TreeView control to true

 

 

in the MainForm.cs [Design]

 

 

window.

 

 

 

 

2

Handle the KeyDown event for

private void treeViewMain_KeyDown

 

the TreeView control to initiate

(object sender, System.Windows.

 

a label edit when the F2 key is

Forms.KeyEventArgs e)

 

{

 

pressed in the tree control.

 

if (e.KeyCode == Keys.F2)

 

 

{

 

 

if (treeViewMain.SelectedNode != null)

 

 

{

 

 

treeViewMain.SelectedNode.BeginEdit();

 

 

e.Handled = true;

 

 

}

 

 

}

 

 

}

 

 

 

3

Update the menuEdit_Popup

private void menuEdit_Popup

 

event handler to use the text

(object sender, System.EventArgs e)

 

“Node” for the

{

 

if (treeViewMain.Focused)

 

menuEditLabel menu when

 

{

 

the TreeView has the focus.

menuEditLabel.Enabled

 

How-to

= (treeViewMain.SelectedNode != null);

 

menuEditLabel.Text = "&Node";

 

Use the Focused property for

}

 

the TreeView class.

else // assume ListView has focus

 

 

{

 

 

menuEditLabel.Enabled

 

 

= (listViewMain.SelectedItems.Count > 0);

 

 

if (this._albumsShown)

 

 

menuEditLabel.Text = "&Name";

 

 

else

 

 

menuEditLabel.Text = "&Caption";

 

 

}

 

 

}

 

 

 

FUN WITH TREE VIEWS

517

SUPPORT EDITING OF TREE NODE LABELS (continued)

 

Action

Result

 

 

 

4

Update the menuEdit-

private void menuEditLabel_Click

 

Label_Click event handler to

(object sender, System.EventArgs e)

 

edit the appropriate item based

{

 

if (treeViewMain.Focused)

 

on the current focus.

 

{

 

 

if (treeViewMain.SelectedNode != null)

 

 

treeViewMain.SelectedNode.BeginEdit();

 

 

}

 

 

else if (listViewMain.SelectedItems.Count > 0)

 

 

listViewMain.SelectedItems[0].BeginEdit();

 

 

}

 

 

 

5

Handle the AfterLabelEdit

private void treeViewMain_AfterLabelEdit

 

event for the TreeView control.

(object sender, System.Windows.

 

 

Forms.NodeLabelEditEventArgs e)

 

How-to

{

 

a. Cancel the edit if the new

if (e.Label == null)

 

{

 

text is null.

 

// Edit cancelled by the user

 

b. Do nothing if the node is a

e.CancelEdit = true;

 

root node.

return;

 

}

 

c. For an album node, use the

 

 

 

UpdateAlbumName.method.

// No changes required for root node

 

d. For a photograph node, use

if (e.Node.Parent == null)

 

return;

 

the UpdatePhotoCaption

 

 

method.

string fileName = e.Node.Tag as string;

 

 

if (Path.GetExtension(fileName) == ".abm")

 

Note: We permit the user to

e.CancelEdit = !UpdateAlbumName(e.Label,

 

e.Node);

 

edit the root node here to alter

 

else

 

a top-level name in the tree,

 

e.CancelEdit = !UpdatePhotoCaption(e.Label,

 

even though this change is dis-

e.Node);

 

carded when the application

}

 

 

 

exits. A more robust solution

 

 

might be to prevent this from

 

 

occurring, or to save the

 

 

change in a configuration file.

 

 

 

 

6

Rewrite the UpdateAlbum-

private bool UpdateAlbumName

 

Name method to accommodate

(string newName, object obj)

 

both list items and tree nodes.

{

 

ListViewItem item = obj as ListViewItem;

 

 

 

How-to

TreeNode node = obj as TreeNode;

 

 

 

a. Change the second parame-

// Determine the file name

 

ter to an object rather than

string fileName = null;

 

a ListViewItem.

if (item != null)

 

b. Convert the given object to

{

 

fileName = item.Tag as string;

 

both a list item and a tree

node = FindNode(fileName, false);

 

node.

}

 

c. Determine the file name for

else if (node != null)

 

fileName = node.Tag as string;

 

the appropriate object.

Note: Recall that the list view’s AfterLabelEdit

 

d. If the object is a list view

 

event handler from chapter 14 provides a ListView-

 

item, also find the node cor-

 

Item object when calling this method. This invocation

 

responding to this item.

 

is still valid and is properly dealt with by this code.

 

 

 

 

 

518

CHAPTER 15 TREE VIEWS

SUPPORT EDITING OF TREE NODE LABELS (continued)

 

Action

Result

 

 

 

7

Rename the file.

// Rename the file

 

How-to

string newFileName = null;

 

if (fileName != null)

 

Use the RenameFile method

{

 

from chapter 14.

newFileName

 

 

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

 

 

}

 

 

if (newFileName == null)

 

 

{

 

 

MessageBox.Show("Unable to rename album "

 

 

+ "to this name.");

 

 

return false;

 

 

}

 

 

 

8

Update the Tag property for the

// Update the appropriate Tag property

 

appropriate object.

if (item != null)

 

Note: When the object is a list

{

 

item.Tag = newFileName;

 

item, this updates the corre-

if (node != null)

 

sponding node as well.

node.Text = newName;

 

}

 

 

 

 

else if (node != null)

 

 

node.Tag = newFileName;

 

 

return true;

 

 

}

 

 

 

9

Rewrite the UpdatePhoto-

private bool UpdatePhotoCaption

 

Caption method to accom-

(string caption, object obj)

 

modate both list items and tree

{

 

ListViewItem item = obj as ListViewItem;

 

nodes.

 

TreeNode node = obj as TreeNode;

 

How-to

// Determine the album index

 

a. Change the second param-

 

int index = -1;

 

eter to an object rather

if ((item != null) && (item.Tag is int))

 

than a ListViewItem.

{

 

index = (int)item.Tag;

 

b. Convert the given object

 

node = FindNode(_album[index].FileName,

 

to both a list item and a

false);

 

tree node.

}

 

else if (node != null)

 

c. Determine the album index

 

{

 

for the appropriate object.

index = node.Index;

 

d. If the object is a list view

}

 

 

 

item, also find the node

 

 

corresponding to this item.

 

 

 

 

10

Return false if the caption

if ((caption.Length == 0) || (index < 0))

 

cannot be updated.

{

 

 

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

 

 

return false;

 

 

}

 

 

 

FUN WITH TREE VIEWS

519

SUPPORT EDITING OF TREE NODE LABELS (continued)

 

Action

Result

 

 

 

11

Update the photograph’s

// Update caption

 

caption, and save the changes

_album[index].Caption = caption;

 

to the album.

if (item != null && node != null)

 

{

 

 

 

Note: When the object is a list

// Update node text as well

 

item, this updates the corre-

node.Text = caption;

 

}

 

sponding node as well.

 

 

 

 

// Save the changes to the album

 

 

. . .

 

 

}

 

 

 

Our program now permits editing of nodes in the TreeView and items in the ListView. Editing is initiated with the menuLabelEdit menu or the F2 key, and is based on which control currently has the focus.

In both update methods, note how the as keyword is used to convert the given object into both a TreeView and a ListView, as is shown in the following excerpt. The remainder of each method executes the appropriate statements based on which type of control is provided.

ListViewItem item = obj as ListViewItem;

TreeNode node = obj as TreeNode;

Also of note is our use of the FindNode method created earlier in the chapter as part of section 15.4.2. As you may recall, we included a parameter to this method that indicated whether to expand the selected node. We set this second parameter to false here to ensure that the contents of the tree view control are not altered.

Our final change is to support the display of our album and photograph property dialogs from the TreeView control.

15.5.3UPDATING THE PROPERTIES MENU

In chapter 14 we created a Properties menu. We handled the Click event for this menu in a menuProperties_Click method, and created the DisplayAlbumProperties and DisplayPhotoProperties methods to display the two types of dialogs

required. Here we would like to change the behavior of this menu to the following:

When the TreeView has the focus, display the appropriate properties dialog if an album node or a photograph node is selected.

When the ListView has the focus, display the appropriate properties dialog for the selected item.

When the PictureBox has the focus, display the photograph properties dialog associated with the displayed image.

To make this change, we will modify our Display methods to accept either a ListViewItem or a TreeNode object. The following table details the changes required.

520

CHAPTER 15 TREE VIEWS

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