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

In our tree view for the MyAlbumExplorer application, we would like to represent each album as a node in the tree, with each album containing a node for each photograph in that album. Since albums can appear in any directory, we might also wish to indicate where a set of albums is located. We will do this by generating a tree structure similar to the one shown in figure 15.6. This tree was generated in Visual Studio to illustrate the hierarchy we will employ. The ListView control in this figure is totally unrelated to the contents of our tree. This is not what we ultimately want, but it is okay for now.

Figure 15.6

In the TreeView, note how the selected album employs a different icon than the unselected one

As an introduction to tree nodes, let’s create the structure shown in figure 15.6 in Visual Studio .NET. The following table details the steps required.

 

 

 

CREATE TREE NODES IN VISUAL STUDIO

 

 

 

 

 

 

 

 

Action

 

 

Result

 

 

 

 

 

 

1

In the MainForm.cs [Design]

 

 

window, set the ImageList

 

 

property of the tree view to use

 

 

the existing imageListSmall

 

 

component already associated

 

 

with the form.

 

 

 

 

 

 

 

 

 

2

Set default index values for

 

 

nodes in the tree.

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

Property

Value

 

 

 

ImageIndex

1

 

 

 

 

SelectedImageIndex

4

 

 

 

 

 

 

 

 

494

CHAPTER 15 TREE VIEWS

CREATE TREE NODES IN VISUAL STUDIO (continued)

 

 

 

 

 

Action

 

 

 

 

Result

 

 

 

 

 

 

 

 

 

 

3

 

Display the TreeNode Editor

 

 

 

dialog box for the control.

 

 

 

How-to

 

 

 

 

 

 

 

 

 

Click the button for the Nodes

 

 

 

entry in the Properties window,

 

 

 

as shown in the graphic for

 

 

 

steps 1 and 2.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4

 

Create a top-level node for the

A top-level Default Albums node appears in the TreeNode

 

 

tree.

 

 

 

 

 

 

Editor. This node is shown in the graphic for step 6.

 

 

How-to

 

 

 

 

 

 

 

 

 

Click the Add Root button.

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Property

 

Value

 

 

 

 

Label

 

Default Albums

 

 

 

 

Image

 

books image

 

 

 

 

Selected Image

books image

 

 

 

 

 

 

 

 

 

 

 

 

 

5

 

Add three child nodes for the

Both nodes appear using the default indexes.

 

 

Default Albums node.

Note: When you select a node, notice how the

 

 

How-to

 

 

 

 

 

 

 

 

 

 

 

 

 

 

selected image assigned to the node is displayed in

 

 

Add each node by clicking the

the tree.

 

 

Add Child button while the

 

 

 

Default Albums node is

 

 

 

selected.

 

 

 

 

 

 

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Node

 

Property

 

Value

 

 

 

 

First

 

Label

 

Album 1

 

 

 

 

Second

 

Label

 

Album 2

 

 

 

 

Third

 

Label

 

Album 3

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

THE TREEVIEW CLASS

495

CREATE TREE NODES IN VISUAL STUDIO (continued)

 

 

Action

Result

 

 

 

 

 

 

6

Add a child node for the Album 1

 

 

node.

 

 

 

 

How-to

 

 

 

 

Click the Add Child button while

 

 

the Album 1 node is selected.

 

 

 

Settings

 

 

 

 

 

 

 

 

 

Property

Value

 

 

 

Label

Photo 1

 

 

 

Image

The normal face

 

 

 

 

image

 

 

 

Selected Image

The smiley face

 

 

 

 

image

 

 

 

 

 

 

 

7

Click the OK button to save the

The nodes are displayed in the designer window.

 

new nodes.

 

 

 

 

 

 

 

 

 

The new nodes appear in the designer window, and are present as we saw in figure 15.6. Run the program and note how the image changes when each node is selected. Also note the plus and minus signs that appear to indicate whether a node is expanded or collapsed.

Let’s take a look at the code generated in the InitializeComponent method. The assignment of the Nodes property is shown, reformatted to be a bit more readable than the code that is generated by Visual Studio.

this.treeViewMain.Nodes.AddRange(new System.Windows.Forms.TreeNode[]

{

new System.Windows.Forms.TreeNode("Default Albums", 5, 5,

new System.Windows.Forms.TreeNode[]

{

new System.Windows.Forms.TreeNode("Album 1",

new System.Windows.Forms.TreeNode[]

{

new System.Windows.Forms.TreeNode("Photo 1", 0, 3)

}),

new System.Windows.Forms.TreeNode("Album 2"), new System.Windows.Forms.TreeNode("Album 3")

})

});

This code uses various forms of the TreeNode constructor to create the nodes in the tree. If you look carefully, you will realize that the Nodes property for the tree contains a single entry, our root Default Albums node. This root node is created to contain an array of three TreeNode objects, namely the Album 1, Album 2, and Album 3 nodes.

496

CHAPTER 15 TREE VIEWS

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