The List View Control

The ListView control allows you to view lists of items with optional associated icons in the manner of Windows Explorer. Using the ListView control, you can display items with large associated icons, small associated icons, or additional details about the item. Table 3-5 shows important properties of the ListView control.

Table 3-5 Important Properties of the ListView Control

Property

Description

Columns

Contains the collection of columns to be displayed when the View property is set to Details.

Groups

Contains an optional collection of groups that can be used to categorize the items contained in the Items collection.

Items

A collection of ListViewItems that is displayed in the ListView control.

LargelmageList

The ImageList component from which images for ListViewItems are drawn when the View property is set to LargeIcon.

ShowGroups

Determines whether the groups contained in the Groups collection are shown.

SmalllmageList

The ImageList component from which images for ListViewItems are drawn when the View property is set to SmallIcon.

View

Indicates the manner in which ListView items are displayed.

The most important property in the ListView control is the Items property. This property contains a collection of ListViewItem objects. Unlike the list-based controls examined earlier, ListViewItems are specific objects that contain additional information about the item being displayed such as icons that are shown in the control. Table 3-6 shows important properties of the ListViewItem class.

Table 3-6 Important Properties of the ListViewItem Class

Property

Description

Group

The group, if any, in the ListView control's Groups collection that this ListViewItem belongs to.

ImageIndex

The index, if any, of the Image to be used for this item when the View property is set to LargeIcon or SmallIcon. If the ImageIndex property is set, the ImageKey property is set to "".

ImageKey

The key of the Image, if any, to be used for this item when the View property is set to LargeIcon or SmallIcon. If the ImageKey property is set, the ImageIndex property is set to null.

SubItems

Contains the sub-items that will be shown when the View property is set to Details. These items should correspond to the columns in the ListView control's Columns collection.

Text

The text that is shown in the ListView property.

You can add ListViewItems to the ListView and edit the properties of individual ListViewItems by clicking the Items property of the ListView control to open the ListViewItem Collection Editor, shown in Figure 3-4.

The ListView control organizes the images associated with the ListViewItems in ImageList objects that are exposed in the SmallImageList and LargeImageList properties. The ImageList class will be discussed in greater detail in Lesson 2, "Creating and Configuring Value-Setting Controls," of this chapter. You can set the images associated with a particular ListViewItem by setting either the ImageIndex or ImageKey property of each ListViewItem. The View property determines if the ListView items are shown with large images, small images, or in a view that exposes the sub-items of the ListViewItems.

Figure 3-4 The ListView Collection Editor

► To display a list of items with icons in the ListView control

1. In the Designer, drag an ImageList control from the Toolbox to the design surface.

2. In the Properties window, click the Images property of the ImageList to add images to the Images collection.

3. In the Designer, select the ListView control. In the Properties window, set the SmallImageList, LargeImageList, or both to the ImageList object.

4. In the Properties window, click Items to add ListViewItems to the ListView.

5. In the ListViewItem Collection Editor, set either the ImageIndex or the ImageKey property for each ListViewItem to the appropriate image in the ImageList. Also, set any other properties, such as Text, at this point.

6. In the Designer, select the ListView control. In the Properties window, set the View property to either LargeIcon or SmallIcon.

Was this article helpful?

0 0

Post a comment