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

Before we actually do this, a utility method to open a PhotoAlbum using a given album node will turn out to be useful here and later on in the chapter. We will create this method first.

CREATE AN OPENTREEALBUM METHOD

 

Action

Result

 

 

 

1

In the MainForm.cs code window,

private PhotoAlbum OpenTreeAlbum

 

create a new OpenTreeAlbum method

(TreeNode node)

 

that accepts a TreeNode object and

{

 

 

 

returns an album.

 

 

 

 

2

Begin this method by opening the

string s = node.Tag as string;

 

album associated with the node.

PhotoAlbum album = OpenAlbum(s);

 

How-to

 

 

Use the OpenAlbum method created in

 

 

chapter 14.

 

 

 

 

3

Update the image index values for this

if (album == null)

 

node.

{

 

 

// Unable to open album

 

How-to

node.ImageIndex = ErrorIndex;

 

a. If the album cannot be opened,

node.SelectedImageIndex = ErrorIndex;

 

}

 

use the error icon for both images.

 

else

 

b. Otherwise, use the standard

{

 

album images.

// Album opened successfully

 

node.ImageIndex = AlbumIndex;

 

 

 

 

node.SelectedImageIndex

 

 

= SelectedAlbumIndex;

 

 

}

 

 

 

4

Return the result of the OpenAlbum

return album;

 

call.

}

 

 

 

With this method in place, we can create a BeforeExpand event handler for our TreeView control. The following table continues the previous steps to create this handler.

HANDLE THE BEFOREEXPAND EVENT

 

Action

Result

 

 

 

5

In the MainForm.cs [Design] window,

private void treeViewMain_BeforeExpand

 

add a BeforeExpand event handler

(object sender, System.Windows.

 

for the TreeView control.

Forms.TreeViewCancelEventArgs e)

 

{

 

 

 

 

TreeNode node = e.Node;

 

 

 

DYNAMIC TREE NODES

503

HANDLE THE BEFOREEXPAND EVENT (continued)

 

Action

Result

 

 

 

6

To implement this handler, see if the

string s = node.Tag as string;

 

expanding node is an album.

if (s == null

 

 

|| (Path.GetExtension(s) != ".abm"))

 

How-to

{

 

a. Convert the Tag property for the

// Not an album node

 

return;

 

node to a string.

 

}

 

b. See if this string has an album file

 

 

extension.

 

 

c. If not, simply return.

 

 

 

 

7

Clear the existing contents of the

// Found an album node

 

node.

node.Nodes.Clear();

 

 

 

8

Open the corresponding PhotoAlbum

using (PhotoAlbum album

 

object for this node.

= OpenTreeAlbum(node))

 

 

{

 

 

Note: Recall that in chapter 5 we supported

 

 

the IDisposable interface in our PhotoAlbum

 

 

class, which allows us to employ the using

 

 

statement here.

 

 

 

9

If the album could not be opened or

// Cancel if null or empty album

 

is empty, then cancel the operation.

if (album == null || album.Count == 0)

 

 

{

 

 

e.Cancel = true;

 

 

return;

 

 

}

 

 

 

10

Otherwise, enumerate through the

// Add a node for each photo in album

 

Photograph objects in the album to

treeViewMain.BeginUpdate();

 

update the contents of the album

foreach (Photograph p in album)

 

{

 

node.

 

 

 

 

 

11

Create a new TreeNode for this

// Create a new node for this photo

 

photo.

TreeNode newNode

 

Note: Set the default and selected

= new TreeNode(

 

album.GetDisplayText(p),

 

image index for the node to use the

MainForm.PhotoIndex,

 

appropriate photograph icon.

MainForm.SelectedPhotoIndex);

 

 

 

 

 

12

Assign the file path for the photo to

newNode.Tag = p.FileName;

 

the Tag property of the new node.

 

 

 

 

13

Add the new node to the Nodes

node.Nodes.Add(newNode);

 

collection of the expanding tree node.

}

 

 

treeViewMain.EndUpdate();

 

 

}

 

 

}

 

 

 

This code returns fairly quickly if the node does not represent an album. Before an album node is expanded, this code adjusts the node and its contents for one of three possible situations:

1If the album cannot be opened, then the ErrorIndex constant is assigned to the ImageIndex and SelectedImageIndex properties via the OpenTree-

504

CHAPTER 15 TREE VIEWS

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