Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Manning - Windows Forms Programming With CSharp.pdf
Скачиваний:
72
Добавлен:
24.05.2014
Размер:
14.98 Mб
Скачать

UPDATE PROPERTIES MENU TO HANDLE TREE NODES

 

Action

 

Result

 

 

 

1

In the MainForm.cs code

private void menuProperties_Click

 

window, update the

(object sender, System.EventArgs e)

 

menuProperties_Click event

{

 

 

if (treeViewMain.Focused)

 

handler to accommodate the

 

{

 

 

three controls that might have

TreeNode node = treeViewMain.SelectedNode;

 

the focus.

string file = node.Tag as string;

 

 

if (node == null || node.Parent == null

 

How-to

|| file == null)

 

a. For the TreeView control,

return;

// do nothing

 

 

 

 

ignore the parent node and

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

 

call the appropriate Proper-

DisplayAlbumProperties(node);

 

ties method based on the

else

 

 

node type.

DisplayPhotoProperties(node);

 

}

 

 

b. For the PictureBox control,

 

 

else if (pictureBoxMain.Focused)

 

call the DisplayPhoto-

{

 

 

Properties method on the

// Display photograph for this image

 

TreeNode node = treeViewMain.SelectedNode;

 

selected photo node.

 

if (node != null)

 

c. For the ListView control,

DisplayPhotoProperties(node);

 

the code is the same as in

}

 

 

else

 

 

chapter 14.

 

 

if (listViewMain.SelectedItems.Count > 0)

 

 

{

 

 

 

ListViewItem item

 

 

= listViewMain.SelectedItems[0];

 

 

if (this._albumsShown)

 

 

DisplayAlbumProperties(item);

 

 

else

 

 

 

DisplayPhotoProperties(item);

 

 

}

 

 

 

}

 

 

 

 

2

Rewrite the DisplayAlbum-

private void DisplayAlbumProperties

 

Properties method to accept

(object obj)

 

 

an object instance.

{

 

 

ListViewItem item = obj as ListViewItem;

 

 

 

How-to

TreeNode node = obj as TreeNode;

 

 

 

 

a. Convert the given object to

// Open the album as appropriate

 

a ListViewItem and a

PhotoAlbum album = null;

 

TreeNode instance.

if (item != null)

 

b. Open the PhotoAlbum using

{

 

 

string fileName = item.Tag as string;

 

whichever object is not

if (fileName != null)

 

null.

album = this.OpenAlbum(fileName);

 

c. If the album could not be

}

 

 

else if (node != null)

 

opened, display an error

{

 

 

message.

album = OpenTreeAlbum(node);

 

 

}

 

 

 

if (album == null)

 

 

. . . // as in chapter 14

 

 

 

 

FUN WITH TREE VIEWS

521

UPDATE PROPERTIES MENU TO HANDLE TREE NODES (continued)

 

Action

Result

 

 

 

3

When displaying the album edit

using (AlbumEditDlg dlg

 

dialog, only update the list item

= new AlbumEditDlg(album))

 

settings if the given item is a list

{

 

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

 

view item.

 

{

 

Note: If the given item is a tree

// Save changes made by the user

 

. . .

 

node, then photographs are dis-

 

// Update item settings

 

played in the list view, and

if (item != null)

 

these settings should not be

{

 

updated.

item.SubItems[MainForm.

 

AlbumTitleColumn].Text

 

 

 

 

= album.Title;

 

 

bool hasPwd = (album.Password != null)

 

 

&& (album.Password.Length > 0);

 

 

item.SubItems[MainForm.

 

 

AlbumPwdColumn].Text

 

 

= (hasPwd ? "y" : "n");

 

 

}

 

 

}

 

 

}

 

 

album.Dispose();

 

 

}

 

 

 

4

Modify the

private void DisplayPhotoProperties

 

DisplayPhotoProperties

(object obj)

 

{

 

method to accept an object

 

ListViewItem item = obj as ListViewItem;

 

instance.

 

TreeNode node = obj as TreeNode;

 

How-to

int index = 0;

 

This is similar, at least in spirit, to

 

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

 

the DisplayAlbumProperties

{

 

method.

index = item.Tag;

 

 

}

 

 

else if (node != null)

 

 

{

 

 

index = node.Index;

 

 

}

 

 

_album.CurrentPosition = index;

 

 

 

522

CHAPTER 15 TREE VIEWS

UPDATE PROPERTIES MENU TO HANDLE TREE NODES (continued)

 

Action

Result

 

 

 

5

After displaying the dialog,

using (PhotoEditDlg dlg

 

update the list or node with any

= new PhotoEditDlg(_album))

 

modified photograph settings.

{

 

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

 

 

 

Note: Recall that our photo edit

{

 

dialog permits all photographs

// Save any changes made

 

. . .

 

in an album to be updated. As a

 

// Update controls with new settings

 

result, when the photographs

TreeNode baseNode = null;

 

are shown in the tree node, the

if (item != null)

 

label for each related node must

{

 

LoadPhotoData(_album);

 

be updated as well. This is true

 

baseNode = treeViewMain.SelectedNode;

 

regardless of the type object

}

 

given.

else if (node != null)

 

 

{

 

 

baseNode = node.Parent;

 

 

}

 

 

if (baseNode != null)

 

 

{

 

 

// Update all child labels

 

 

foreach (TreeNode n in baseNode.Nodes)

 

 

{

 

 

n.Text = _album[n.Index].Caption;

 

 

}

 

 

}

 

 

}

 

 

}

 

 

}

 

 

 

As you can see, the display methods use the as keyword to convert a given object into both a ListViewItem and a TreeNode instance. Whichever instance is non- null indicates how to display the property dialog.

TRY IT! As a further change to our TreeView control, add a context menu to this control to perform the following tasks.

1An “Add Directory” menu item that permits a new album directory to be added to the tree. This should prompt for a directory name and add a top-level node to the tree for each album discovered in that directory.

2A “Properties” menu item that displays the properties dialog for the nearest node. This should select the nearby node, and then call the

PerformClick method for the menuProperties menu.

3A “Delete” menu item that deletes a node from the tree. This should

delete the album file from the file system or the Photograph from the containing album for the given node. You should prompt the user to make sure they really wish to do this.

You will need to use the GetNodeAt method to locate the TreeNode instance at a given pixel position, so that the action applies to the specific tree node located at the current mouse position.

FUN WITH TREE VIEWS

523

You could also implement these items within the ListView control as well. This completes our discussion on the TreeView class. Before we move on, let’s do a quick recap of what we covered in this chapter.

15.6RECAP

In this chapter we extended the MyAlbumExplorer project built in chapter 14 to add a TreeView control. We divided our main window using the Splitter class in order to create a classic explorer window such as that used in the Windows operating system for browsing the file system.

A tree view contains a hierarchy of TreeNode objects, and we created a tree displaying our album files and the photos in each album. We discussed common operations within a tree view such as expand, collapse, selection, and label editing. During the course of the chapter, the ListView and TreeView controls were integrated to display a common interface, with changes to one control reflected in the other control. We also added a PictureBox control in order to display the image associated with a selected photograph node in the tree.

The explorer interface we saw in these last two chapters is one of three kinds of standard Windows interfaces. In part 2 of this book we built what is called a single document interface. In the next chapter we will look at another kind of interface, namely the multiple document interface.

524

CHAPTER 15 TREE VIEWS