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

14.5.7REDISPLAYING THE ALBUMS

As a final change, we need to give our user the opportunity to redisplay the album view. We may as well provide a menu to display the photo view as well, as an alternative to double-clicking on the album.

ALLOW USER SELECTION OF THE KIND OF OBJECT TO DISPLAY

 

 

 

Action

 

 

Result

 

 

 

 

 

 

 

1

 

In the MainForm.cs [Design]

 

 

 

window, add three menu items

 

 

 

to the bottom of the View menu.

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

Menu

Property

 

Value

 

 

 

separator

Text

 

-

 

 

 

 

Albums

(Name)

 

menuAlbums

 

 

 

 

Text

 

&Albums

 

 

 

Photos

(Name)

 

menuPhotos

 

 

 

 

Text

 

&Photos

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2

 

Add a Click handler for the

private void menuAlbums_Click

 

 

Albums menu.

 

 

(object sender, System.EventArgs e)

 

 

 

 

 

 

 

{

 

 

 

 

 

 

 

// Display albums in the list

 

 

 

 

 

 

 

if (!_albumsShown)

 

 

 

 

 

 

 

{

 

 

 

 

 

 

 

LoadAlbumData(PhotoAlbum.DefaultDir);

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

3

 

Add a Click handler for the

private void menuPhotos_Click

 

 

Photos menu.

 

 

(object sender, System.EventArgs e)

 

 

Note: This is the same as acti-

{

 

 

// Activate the selected album

 

 

vating an album item.

listViewMain_ItemActivate(sender, e);

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

4

 

Update the Popup handler for

private void menuView_Popup

 

 

the View menu to enable or

(object sender, System.EventArgs e)

 

 

disable the Photos menu as

{

 

 

View v = listViewMain.View;

 

 

appropriate.

 

 

 

 

 

 

. . .

 

 

 

 

 

 

 

if (_albumsShown && listViewMain.

 

 

 

 

 

 

 

SelectedItems.Count > 0)

 

 

 

 

 

 

 

menuPhotos.Enabled = true;

 

 

 

 

 

 

 

else

 

 

 

 

 

 

 

menuPhotos.Enabled = false;

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

14.6RECAP

This completes our discussion of the ListView class. In this chapter we discussed list views in detail, and created a new MyAlbumExplorer interface to display the collection of albums available in our default album directory. We supported all four possible views available in a ListView control, and provided support for column

RECAP

483

sorting, item selection, and label editing. We finished by implementing this same support for the photos in an album, so that our application can display albums or photographs in the control.

Along the way we looked at a number of classes provided to support this control, most notably the ListViewItem, ListViewItem.ListViewSubItem, and ColumnHeader classes. We also examined the IComparer interface as a way to define how two objects should be compared, and implemented a class supporting this interface in order to sort the columns in our detailed view of the list.

The next chapter looks at a close cousin to the ListView class, namely the

TreeView control.

484

CHAPTER 14 LIST VIEWS

C H A P T E R

1 5

 

 

Tree views

15.1

Tree view basics 486

15.4

Node selection 505

15.2

The TreeView class 486

15.5

Fun with tree views 513

15.3

Dynamic tree nodes 497

15.6

Recap 524

In the previous chapter we created the MyAlbumExplorer application incorporating a ListView control. This program presents the default set of photo albums available and the collection of photographs contained within these albums.

In this chapter we extend this program to include a TreeView control in order to present a more traditional explorer-style interface. Specific topics we will cover in this chapter include the following:

Exploring the TreeView class.

Using the Splitter control to divide a container.

Populating a tree with the TreeNode class, both in Visual Studio and programmatically.

Selecting nodes in a tree.

Editing the labels for a tree.

Integrating a ListView and TreeView control into an application.

As we did for list views, we begin this chapter with a general discussion of tree views and a discussion of the terms and classes used for this control.

485

15.1TREE VIEW BASICS

The TreeView class is a close cousin of the ListView class. List views display a collection as a list, while tree views display collections as a tree. Each item in a tree view is called a tree node, or just a node. Tree nodes can contain additional nodes, called child nodes, to arbitrary levels in order to represent a hierarchy of objects in a single control. Various elements of a TreeView control are illustrated in figure 15.1.

 

b An icon taken from an

 

ImageList instance is

 

associated with each node.

b

c An alternate icon from the

c

ImageList can be displayed

 

when a node is selected.

d

d The primary text

associated with each

 

node is called the

 

node label.

e

e The TreeNode class

represents a single

 

element, or node,

 

in the list.

Figure 15.1 The TreeView control automatically shows the entire label in a tool tip style format when the mouse hovers over a node, as was done for the “From the Walking Path” entry in this figure.

The explorer-style interface shown in the figure and used by other applications such as Windows Explorer is a common use of the TreeView and ListView classes. In this chapter we build such an interface by extending the MyAlbumExplorer project constructed in chapter 14.

15.2THE TREEVIEW CLASS

The TreeView class is summarized in.NET Table 15.1. Like the ListView class, this class inherits directly from the Control class, and provides an extensive list of members for manipulating the objects displayed by the tree.

486

CHAPTER 15 TREE VIEWS

.NET Table 15.1 TreeView class

The TreeView class represents a control that displays a collection of labeled items as a treestyle hierarchy. Typically an icon is displayed for each item in the collection to provide a graphical indication of the nature or purpose of the item. Items in the tree are referred to as nodes, and each node is represented by a TreeNode class instance. This class is part of the System.Windows.Forms namespace, and inherits from the Control class. See .NET Table 4.1 on page 104 for a list of members inherited by this class.

 

CheckBoxes

Gets or sets whether check boxes are displayed next to each

 

 

node in the tree. The default is false.

 

HideSelection

Gets or sets whether a selected node remains highlighted

 

 

even when the control does not have focus.

 

ImageIndex

Gets or sets an index into the tree’s image list of the default

 

 

image to display by a tree node.

 

ImageList

Gets or sets an ImageList to associate with this control.

 

LabelEdit

Gets or sets whether node labels can be edited.

 

Nodes

Gets the collection of TreeNode objects assigned to the

Public

 

control.

Properties

PathSeparator

Gets or sets the delimiter used for a tree node path, and in

 

 

 

particular the TreeNode.FullPath property.

 

SelectedNode

Gets or sets the selected tree node.

 

ShowPlusMinus

Gets or sets whether to indicate the expansion state of

 

 

parent tree nodes by drawing a plus ‘+’ or minus ‘-‘ sign next

 

 

to each node. The default is true.

 

Sorted

Gets or sets whether the tree nodes are sorted alphabetically

 

 

based on their label text.

 

TopNode

Gets the tree node currently displayed at the top of the tree

 

 

view control.

 

 

 

 

CollapseAll

Collapses all the tree nodes so that no child nodes are visible.

Public

GetNodeAt

Retrieves the tree node at the specified location in pixels

 

within the control.

Methods

 

 

 

 

GetNodeCount

Returns the number of top-level nodes in the tree, or the total

 

 

number of nodes in the entire tree.

 

 

 

 

AfterExpand

Occurs after a tree node is expanded.

 

AfterLabelEdit

Occurs after a tree node label is edited.

Public

BeforeCollapse

Occurs before a tree node is collapsed.

Events

 

 

 

BeforeSelect

Occurs before a tree node is selected.

 

ItemDrag

Occurs when an item is dragged in the tree view.

 

 

 

A TreeView object is created much like any other control in Visual Studio .NET: you simply drag the control onto the form. In our MyAlbumExplorer application, we already have a ListView on our form, so it looks like all we need to add is a tree view in order to support the interface shown in figure 15.2.

THE TREEVIEW CLASS

487

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