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

UPDATE THE MENUALBUMS_CLICK EVENT HANDLER

 

Action

Result

 

 

 

1

Locate the menuAlbums_Click event

private void menuAlbums_Click

 

handler in the MainForm.cs code

(object sender, System.EventArgs e)

 

window.

{

 

 

 

 

 

2

Modify this handler to select the Default

// Select Default Albums node

 

Albums tree node.

if (treeViewMain.Nodes.Count > 0)

 

How-to

{

 

treeViewMain.SelectedNode

 

Set the SelectedNode property for the

= treeViewMain.Nodes[0];

 

tree to the first node in the tree.

}

 

}

 

 

 

 

 

Since we initialize the tree with a top-level node, we know this will always exist and appear first in the tree view object’s Nodes collection. We select this by assigning this node to the SelectedNode property of the tree.

For the Photos menu, you may recall that we created a menuView_Popup event handler that enables this menu only if an album is selected in the ListView control. The existing menuPhotos_Click event handler, shown in the following code, already activates the selected item. This behavior works just fine for our current application, so no changes are required to this handler.

private void menuPhotos_Click(object sender, System.EventArgs e)

{

// Activate the selected album listViewMain_ItemActivate(sender, e);

}

The final event handler, the listViewMain_ItemActivate method, requires some discussion. Our existing handler, shown in the following code, only permits albums to be activated. This handler retrieves the selected item, opens the album file corresponding to the item, and calls LoadPhotoData to display the photographs in the album.

private void listViewMain_ItemActivate(object sender, EventArgs e)

{

if (_albumsShown && listViewMain.SelectedItems.Count > 0)

{

ListViewItem item = listViewMain.SelectedItems[0]; string fileName = item.Tag as string;

// Open the album for this item PhotoAlbum album = null;

if (fileName != null)

album = OpenAlbum(fileName); if (album == null)

{

MessageBox.Show(

"The photographs for this album cannot be displayed.");

510

CHAPTER 15 TREE VIEWS

return;

}

// Switch to a photograph view LoadPhotoData(album);

}

}

In our new code, we will permit any type of item to be activated. Albums will display the photos in the album, and photographs will display a blank list, which we will update shortly to display the actual image. Our logic to select the TreeNode corresponding to the item will go something like this:

private void listViewMain_ItemActivate(object sender, EventArgs e)

{

if (listViewMain.SelectedItems.Count > 0)

{

//Find the file path for the selected item

//Find the tree node with an identical path

//Select the node to activate it

}

}

We will need some assistance with the first two steps. As you may recall, we utilized the Tag property in chapter 14 to store the file name of album items and the index of photograph items. We can use this property to retrieve the path for either type item.

Our next step is to locate the node which matches a given file path. This is a little trickier than it seems, since the node may not yet exist. There are two critical observations we can make in order to properly implement this functionality:

First, the node corresponding to the parent of the activated item will already be selected in the tree. We ensure that a node is selected at all times in our tree, so we can count on this fact to identify the Nodes collection containing our desired node.

Second, the matching node may not actually exist. For example, if a user activates a photograph, a node for the photograph will only exist if the album node containing the photo has been expanded. As a result, we must expand the parent node before we search for a matching node to ensure that the node exists.

With these facts in mind, we are ready to implement a method to locate a node, which we will call FindNode.

NODE SELECTION

511

 

IMPLEMENT A FINDNODE METHOD

 

 

 

 

Action

Result

 

 

 

3

Create a new FindNode

private TreeNode FindNode

 

method.

(string fileName, bool expandNode)

 

Note: This method accepts a

{

 

 

 

file name and returns the

 

 

matching TreeNode object, if

 

 

any.

 

 

This also accepts a boolean

 

 

value indicating whether to

 

 

expand the node. This feature

 

 

will come in handy later in the

 

 

chapter.

 

 

 

 

4

Make sure the selected node is

TreeNode node = treeViewMain.SelectedNode;

 

not null.

if (node == null)

 

Note: This value should never

return null;

 

 

 

be null, but it is always good

 

 

to check.

 

 

 

 

5

If expandNode is true, make

// Ensure contents of node are available

 

sure the contents of the

if (expandNode)

 

selected node are loaded into

node.Expand();

 

 

 

the tree.

 

 

 

 

6

Find the node that matches the

// Search for a matching node

 

given string.

foreach (TreeNode n in node.Nodes)

 

 

{

 

How-to

string nodePath = n.Tag as string;

 

a. For each child of the selected

if (nodePath == fileName)

 

{

 

node, find the file associated

 

// Found it!

 

with the node.

return n;

 

b. If a match is found, return it to

}

 

 

 

the caller.

 

 

 

 

7

If no match is found, return

}

 

null.

return null;

 

 

 

 

}

 

 

 

With these changes in place, we can revamp our ItemActivate handler to select the corresponding tree node.

REIMPLEMENT THE ITEMACTIVATE EVENT HANDLER

 

Action

Result

 

 

 

8

Replace the ItemActivate event

private void listViewMain_ItemActivate

 

handler in the MainForm.cs code

(object sender, System.EventArgs e)

 

window.

{

 

 

 

 

 

512

CHAPTER 15 TREE VIEWS

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