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

REIMPLEMENT THE ITEMACTIVATE EVENT HANDLER (continued)

 

Action

Result

 

 

 

9

If an item is selected, locate the file

if (listViewMain.SelectedItems.Count > 0)

 

name associated with this item.

{

 

 

// Find the file path for selected item

 

How-to

string fileName = null;

 

a. If albums are displayed, the Tag

ListViewItem item

 

= listViewMain.SelectedItems[0];

 

property contains the album

 

if (_albumsShown)

 

path.

{

 

b. If photographs are displayed, the

// Get the file for this album

 

fileName = item.Tag as string;

 

Tag property contains the index

 

}

 

of this photo in the album.

else if (item.Tag is int)

 

 

{

 

 

// Use the index of the photograph

 

 

int index = (int)item.Tag;

 

 

fileName = _album[index].FileName;

 

 

}

 

 

 

10

If no file name is present, the item

if (fileName == null)

 

cannot be activated.

{

 

 

MessageBox.Show("This item cannot "

 

 

+ "be opened.");

 

 

return;

 

 

}

 

 

 

11

If a file name is found, locate the

// Find tree node with identical path

 

TreeNode corresponding to this

TreeNode node

 

item.

= FindNode(fileName, true);

 

 

 

 

 

12

If the node is found,

if (node != null)

 

a. Make sure the node is visible.

{

 

// Select the node to activate it.

 

b. Select the node.

node.EnsureVisible();

 

 

treeViewMain.SelectedNode = node;

 

 

}

 

 

}

 

 

}

 

 

 

Our two view controls are now totally in sync with each other. The appropriate tree node is always selected, and as a result the contents of the list view are updated as required.

15.5FUN WITH TREE VIEWS

There are a few loose ends to tie up in our application. In this section we look at additional uses for a tree view class in order to complete the functionality required in the MyAlbumExplorer application. This section is to demonstrate various features and functionality, rather than explain additional Windows Forms concepts. As a result, this section will be short on discussion and simply present the code required to make the described changes.

There are three changes we will make here:

1Display the image associated with a selected photograph node.

2Permit the label text for a node to be edited.

3Display the album or photo property dialog associated with a node.

FUN WITH TREE VIEWS

513

We will discuss these topics in the order they appear in this list.

15.5.1DISPLAYING THE PHOTOGRAPH

Our first topic is displaying the photograph when a photograph node is selected in the tree view. You might think that we could draw directly on the ListView control. In fact, the ListView class does not permit the Paint event to be handled by an instance of the class. So an alternate approach is required.

Instead, we will use a PictureBox control for this purpose. Since the PictureBox control does not support a proper aspect ratio for its contained image, we will

handle the Paint event and draw the image manually. This is shown in figure 15.9. When a list of albums or photographs is displayed, we will hide the picture box control. Conversely, when a photograph is displayed, we will hide the list view control.

Figure 15.9

Normally a PictureBox control appears with standard control colors. Since this PictureBox appears in place of a ListView control, we will use standard window colors instead.

The following table details the steps necessary to add this feature to our interface.

Set the version number of the MyAlbumExplorer application to 15.4.

DISPLAY PHOTOGRAPH IN A PICTUREBOX CONTROL

 

 

 

Action

Result

 

 

 

 

 

 

1

In the MainForm.cs [Design]

 

 

window, add a PictureBox control

 

 

to the area where the ListView

 

 

control is already located.

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

Property

 

Value

 

 

 

(Name)

 

pictureBoxMain

 

 

 

BackColor

 

Window (under

 

 

 

 

 

the System tab)

 

 

 

BorderStyle

 

Fixed3D

 

 

 

Dock

 

Fill

 

 

 

Visible

 

False

 

 

 

 

 

 

 

 

514

CHAPTER 15 TREE VIEWS

DISPLAY PHOTOGRAPH IN A PICTUREBOX CONTROL (continued)

 

Action

Result

 

 

 

2

In the MainForm.cs code window,

private void DisplayPhoto(TreeNode node)

 

add a new DisplayPhoto

{

 

method.

if (node == null)

 

{

 

 

 

Note: We will use the Tag prop-

pictureBoxMain.Visible = false;

 

erty for the PictureBox control

listViewMain.Visible = true;

 

return;

 

to hold the photo to display, if any.

 

}

 

How-to

// Parent of photo node is album node

 

a. If the given node is null, hide

 

string file = node.Parent.Tag as string;

 

the picture box and display the

if (_album == null

 

ListView control.

|| (_album.FileName != file))

 

{

 

b. If a node was given, ensure the

 

if (_album != null)

 

PhotoAlbum containing the

_album.Dispose();

 

photo is open.

_album = OpenTreeAlbum(node.Parent);

 

c. Assign the Photograph to

 

}

 

display to the PictureBox.Tag

if (_album != null)

 

property.

 

{

 

d. Make the PictureBox visible.

 

// Proper PhotoAlbum is now open

 

 

pictureBoxMain.Tag = _album[node.Index];

 

 

pictureBoxMain.Invalidate();

 

 

pictureBoxMain.Visible = true;

 

 

listViewMain.Visible = false;

 

 

}

 

 

}

 

 

 

3

Create a Pen object in the

private static Pen borderPen

 

MainForm class for drawing a

= new Pen(SystemColors.WindowFrame);

 

border around a photo.

 

 

 

 

4

Add a Paint event handler for the

private void pictureBoxMain_Paint

 

PictureBox control to draw the

(object sender, System.Windows.

 

assigned Photograph in the

Forms.PaintEventArgs e)

 

{

 

PictureBox client area with the

 

Photograph photo

 

proper aspect ratio.

= pictureBoxMain.Tag as Photograph;

 

How-to

if (photo == null)

 

a. Retrieve the Photograph

{

 

object stored in the picture box.

// Something is wrong, give up

 

e.Graphics.Clear(pictureBoxMain.

 

b. If a photograph is not found,

 

BackColor);

 

simply clear the client area.

return;

 

c. Otherwise, use the

}

 

 

 

ScaleToFit method to deter-

// Paint the photograph

 

mine the proper drawing rect-

Rectangle rect = photo.ScaleToFit(

 

angle.

pictureBoxMain.ClientRectangle);

 

e.Graphics.DrawImage(photo.Image, rect);

 

d. Draw the assigned image into

 

e.Graphics.DrawRectangle(borderPen, rect);

 

this rectangle.

}

 

e. Draw a border around the

 

 

image using the Pen object

 

 

created in the previous step.

 

 

 

 

FUN WITH TREE VIEWS

515

NodeLa-

DISPLAY PHOTOGRAPH IN A PICTUREBOX CONTROL (continued)

 

Action

Result

 

 

 

5

Update the AfterSelect event

private void treeViewMain_AfterSelect(. . .)

 

handler to use the new

{

 

DisplayPhoto method to ensure

. . .

 

if (node.Parent == null)

 

the proper control is visible.

 

{

 

 

// Bad tag or top-level node.

 

 

LoadAlbumData(fileName);

 

 

DisplayPhoto(null);

 

 

}

 

 

else if (Path.GetExtension(fileName) . . .)

 

 

{

 

 

// Album node selected

 

 

PhotoAlbum album = OpenTreeAlbum(. . .);

 

 

LoadPhotoData(album);

 

 

DisplayPhoto(null);

 

 

}

 

 

else // must be a photograph

 

 

{

 

 

// Clear the list and display the photo

 

 

listViewMain.Clear();

 

 

DisplayPhoto(node);

 

 

}

 

 

}

 

 

 

6

Add a Resize event handler for

private void pictureBoxMain_Resize

 

the PictureBox control to force

(object sender, System.EventArgs e)

 

the control to redraw the entire

{

 

// Force the entire control to repaint

 

image when it is resized.

 

pictureBoxMain.Invalidate();

 

 

}

 

 

 

As we mentioned at the start of this section, we will not spend much time discussing these changes, since they leverage concepts and features we have seen before. Let’s move on to editing a tree node’s label.

15.5.2SUPPORTING LABEL EDITS

Tree nodes can be edited in a manner similar to list items. There is a BeginEdit method in the TreeNode class to initiate a label edit programmatically, and Befor-

eLabelEdit and AfterLabelEdit events in the TreeView class that occur before and after the user edits the label. Event handlers for these events receive the belEditEventArgs class for the event parameter. This class is summarized in

.NET Table 15.5, and is manipulated in much the same way as we saw for the LabelEditEventArgs class when handling label events for the ListView class.

516

CHAPTER 15 TREE VIEWS

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