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

P A R T 3

Advanced Windows Forms

If you have actually read this book from the beginning, then I applaud your fortitude and welcome you to the third and final part of this book. For those readers who have jumped directly to this page, I would encourage you to actually read the earlier chapters, as they build a foundation for much of the discussion that will occur in this part of the book. Of course, if you are browsing this book with the idea of buying it, then feel free to look around.

In part 3 we look at what might be considered advanced topics. If you have a firm, or at least decent, grasp of the material from part 2 of this book, then this section should be quite understandable.

Chapter 14 kicks off our discussion with the topic of “List views.” This chapter creates a new MyAlbumExplorer application incorporating a ListView control, and demonstrates various means of displaying and interacting with objects in this control.

Chapter 15 on “Tree views” extends the MyAlbumExplorer application to support a standard explorer-style interface. The Splitter and TreeView controls are discussed, and various interactions between the ListView and TreeView controls in the MyAlbumExplorer application are examined.

Chapter 16 turns to the topic of “Multiple document interfaces.” This chapter discusses the support provided by the .NET Framework for multiple document interface, or MDI, applications in Windows Forms. Here we return to our MyPhotos application from part 2 and convert it into an MDI application, using our MainForm class as the child window.

The topic of “Data binding” is taken up in chapter 17. This discusses complex data binding by way of the DataGrid control, and simple binding of data to Windows Forms controls in general. This chapter will illustrate how to provide transactional updates within a class and automatically invoke these updates from a bound control. A new MyAlbumData application is constructed over the course of this chapter.

Chapter 18 is called “Odds and ends .NET,” and completes our discussion with a review of various topics that should be of further interest. These include printing, Windows Forms timers, drag and drop, and ActiveX controls. An example for each topic is provided using the MyPhotos MDI application built in chapter 16.

Following this last chapter are four appendices with some additional information on C#, an overview of .NET namespaces, a class hierarchy chart of the Windows Forms namespace, and resources for additional information on C# and the .NET Framework.

438

PART 3 ADVANCED WINDOWS FORMS

C H A P T E R

1 4

 

 

List views

14.1

The nature of list views 440

14.4

Selection and editing 464

14.2

The ListView class 443

14.5

Item activation 472

14.3

ListView columns 453

14.6

Recap 483

To kick off the advanced section of the book, we take a detailed look at the ListView class. This class is used by applications such as Windows Explorer to present a collection of items in list form. We will examine this class in detail, including the following topics:

Various styles supported by the ListView class.

Members of the ListView class.

Defining list view columns in Visual Studio and programmatically.

Selecting and editing items in the list.

Activating list view items.

Dynamically switching the contents of a list view.

We will start from scratch here and build a new application called MyAlbumExplorer. In this chapter we will display both albums and photographs in the main window. The next chapter will add support for a TreeView control to this application to create a window much like the Windows Explorer application utilizes for file system objects.

439

14.1THE NATURE OF LIST VIEWS

In many ways, a list view is a more glamorous version of a list box. Other than the fact that they are both controls, there is no relation from a class hierarchy perspective, but conceptually both present a scrollable list to the user. The ListBox class stores a collection of object instances, while the ListView class contains a collection of ListViewItem instances, which in turn contains a collection of ListViewSubItem objects.

Another difference is how their contents are displayed. The ListBox control displays a string associated with each object by default, and supports an owner-drawn style to display other formats. The ListView control displays its items in one of four views represented by the View enumeration, as described by .NET Table 14.1. When the Details view is displayed, the collection of subitems appears in a configured set of ColumnHeader objects associated with the control.

.NET Table 14.1 View enumeration

The View enumeration specifies the different ways the contents of a ListView control can appear. This enumeration is part of the System.Windows.Forms namespace. The following table provides an example obtained from the Windows Explorer application.

LargeIcon Each item appears as a large icon with a label below it. By default, items can be dragged around and placed at any location within the control.

Enumeration

 

Values

SmallIcon Each item appears as a small icon with a label at the right. By

 

default, items can be dragged and placed at any location in the

 

control.

440

CHAPTER 14 LIST VIEWS

.NET Table 14.1 View enumeration

List

Items are arranged as small icons, in columns with no headers,

 

and with the labels on the right.

Enumeration

Details

Items are arranged in columns with headers. Items appear as

Values

 

small icons with the labels on the right, and additional

 

 

 

 

information about each item appears in the columns.

Figure 14.1 shows a Form with a ListView control displayed in the Details view mode. This figure illustrates various features and classes used by this control. We will look at these in detail as we progress through the chapter.

An icon taken from an ImageList instance is associated with each item.

The primary text associated with each item is called the item label.

The ListViewItem class represents a single item in the list.

Multiple instances of the

ListViewSubItem class represent additional information associated with each item.

The ColumnHeader class represents a single column for the list.

b

f

c

d b

c

e

d

f e

Figure 14.1 This graphic illustrates important classes and terms used for the ListView control.

THE NATURE OF LIST VIEWS

441

.NET Table 14.2 ListView class

The ListView class is a control that displays a collection of labeled items as a list in one of four different views. 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 can be displayed with large icons, small icons, in a list format, or in a detailed list format. The detailed list permits additional information about each item to appear in columns within the control. 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.

 

Activation

Gets or sets how an item is activated, and whether the font

 

 

changes as the mouse passes over the item.

 

CheckBoxes

Gets or sets whether a check box is displayed next to each

 

 

item. Default is false.

 

Columns

Gets the collection of ColumnHeader components

 

 

associated with the control.

 

HeaderStyle

Gets or sets the column header style for the control.

 

 

Default is ColumnHeaderStyle.Clickable.

 

Items

Gets the collection of items in the list.

 

LabelEdit

Gets or sets whether the user can edit item labels in the

 

 

list. Default is false.

Public

LargeImageList

Gets or sets the ImageList for the LargeIcon view.

Properties

ListViewItemSorter

Gets or sets an IComparer interface to use when sorting

 

 

 

items in the list.

 

MultiSelect

Gets or sets whether multiple items in the list may be

 

 

selected at the same time. Default is false.

 

SelectedItems

Gets the collection of items selected in the list.

 

SmallImageList

Gets or sets the ImageList instance for the views other

 

 

than the LargeIcon view.

 

Sorting

Gets or sets how items in the list are sorted, if at all.

 

StateImageList

Gets or sets the ImageList list for state icons.

 

View

Gets or sets the current View enumeration value for the

 

 

list. Default is LargeIcon.

 

 

 

Public

Clear

Removes all items and columns from the list view control.

EnsureVisible

Ensures a given item is visible, scrolling it into view if

Methods

 

necessary.

 

 

 

 

 

 

AfterLabelEdit

Occurs after an item label has been edited.

 

ColumnClick

Occurs when the user clicks a column header in the

 

 

Details view.

Public

ItemActivate

Occurs when an item is activated. How this occurs

Events

 

depends on the Activation property.

 

ItemDrag

Occurs when a user begins dragging an item in the list.

 

SelectedIndex-

Occurs when the selection state of an item changes.

 

Changed

 

 

 

 

442

CHAPTER 14 LIST VIEWS

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