ListView

Jump to: navigation, search

A ListView control allows you to display a list of items with an optional icon in a similar manner to Windows Explorer. The most often used manner is to show a multicolumn list of items as displayed here.

Image:526-91.jpg

However, the ListView control supports five distinct modes of viewing, very similar in nature to those that can be achieved using Windows Explorer. This is done through the View property.

Contents


Useful properties

  • Activation - Defines the type of action the user must take to activate an item. Takes a value from the ItemActivation enumeration:
  • OneClick - The user must single-click to activate items. The cursor changes to a hand pointer cursor, and the item text changes colour as the user moves the mouse pointer over the item.
  • Standard - The user must double-click to activate items. No feedback is given as the user moves the mouse pointer over an item. (default value)
  • TwoClick - The user must click an item twice to activate it. This is different from the standard double-click because the two clicks can have any duration between them. The item text changes colour as the user moves the mouse pointer over the item.
  • Alignment - Defines the alignment of items in the control when the View property is set to LargeIcon or SmallIcon. Takes a value from the ListViewAlignment enumeration:
  • Default - When the user moves an item, it remains where it is dropped.
  • Left - Items are aligned to the left of the ListView control.
  • SnapToGrid - Items are aligned to an invisible grid in the control. When the user moves an item, it moves to the closest juncture in the grid.
  • Top - Items are aligned to the top of the ListView control. (Default setting)
  • AllowColumnReorder - Indicates whether the user can drag column headers to reorder columns in the control at run time when the View property is set to Details.
  • AutoArrange - Determines whether icons are automatically kept arranged and snapped to the grid when the View property is set to LargeIcon or SmallIcon.
  • CheckBoxes - Indicates whether a check box appears next to each item in the control.
  • CheckedIndices - Gets the indexes of the currently checked items in the control.
  • CheckedItems - Gets the currently checked items in the control.
  • Columns - Gets the collection of all column headers that appear in the control when the View property is set to Details.
Note:
If your ListView control does not have any column headers specified and you set the View property to Details, the ListView control will not display any items.
  • FullRowSelect - Indicates whether clicking an item selects all its sub-items when the View property is set to Details.
  • Groups - Gets the collection of ListViewGroup objects assigned to the control. The ListView grouping feature lets you create visual groups of logically related ListView items. Each group consists of a textual header followed by a horizontal line and the items assigned to that group.
  • HeaderStyle - Specifies the type of column headers to display when the View property of the ListView control is set to Details and the ListView control has ColumnHeader objects specified in the ListView.ColumnHeaderCollection. Takes a value from the ColumnHeaderStyle enumeration:
  • Clickable - The column headers function like buttons and can carry out an action, such as sorting, when clicked.
  • Nonclickable - The column headers do not respond to the click of a mouse.
  • None - The column header is not displayed in report view.
  • HideSelection - Indicates whether the selected item in the control remains highlighted when the control loses focus.
  • HotTracking - Indicates whether the text of an item or sub-item has the appearance of a hyperlink when the mouse pointer passes over it.
Note:
Enabling hot tracking will cause the Activation property to be set to OneClick and the HoverSelection property to be set to true. In addition, run-time label editing is not allowed when hot tracking is enabled.
  • HoverSelection - Indicates whether an item is automatically selected when the mouse pointer remains over the item for a few seconds. When this property is set to true, the user can point to an item in the ListView control to select the item. Multiple items can be selected (when the MultiSelect property is set to true) by holding down the control key while pointing to each item.
  • InsertionMark - Gets a ListViewInsertionMark object used to indicate the expected drop location when an item is dragged within a ListView control. This lets you visually indicate the expected drop location in a drag-and-drop operation when an item is dragged to a new position. This feature works only when the AutoArrange property is set to true and when the ListView control does not sort the items automatically.
  • Items - Gets a collection containing all of the ListViewItems in the control.
  • LabelEdit - Indicates whether the user can edit the labels of items in the control. When set to true, the user is able to modify the text of an item. If the Activation property is set to OneClick or TwoClick, label editing will not be allowed.
  • LabelWrap - Indicates whether item labels wrap when items are displayed in the control as icons.
  • LargeImageList - Defines the ImageList to use when displaying items as large icons in the control.
  • MultiSelect - Indicates whether multiple items can be selected.
  • Scrollable - Indicates whether a scroll bar is added to the control when there is not enough room to display all items.
  • SelectedIndices - Gets the indexes of the selected items in the control.
  • SelectedItems - Gets the items that are selected in the control.
  • ShowGroups - Indicates whether items are displayed in groups.
  • ShowItemToolTips - Indicates whether ToolTips are shown for the ListViewItem objects contained in the ListView.
  • TileSize - Defines the size of the tiles shown in tile view.
  • TopItem - Defines the first visible item in the control.
  • View - Defines how items are displayed in the control. Takes a value from the View enumeration:
  • Details - Each item appears on a separate line with further information about each item arranged in columns. The left-most column contains a small icon and label, and subsequent columns contain sub items as specified by the application. A column displays a header which can display a caption for the column. The user can resize each column at run time.
  • LargeIcon - Each item appears as a full-sized icon with a label below it.
  • List - Each item appears as a small icon with a label to its right. Items are arranged in columns with no column headers.
  • SmallIcon - Each item appears as a small icon with a label to its right.
  • Tile - Each item appears as a full-sized icon with the item label and subitem information to the right of it. The subitem information that appears is specified by the application. This view is available only on Windows XP and the Windows Server 2003 family. On earlier operating systems, this value is ignored and the ListView control displays in the LargeIcon view.


ListView Tasks

When you select the ListView and click on the Smart Tag icon Image:SmartTagGlyph.jpg, a small menu appears with the list of tasks that can be achieved through it.

Image:526-92.jpg


The top-most task allows you to Edit Items, which is a collection containing all of the ListViewItems in the control. The ListViewItem class defines the appearance, behavior, and data associated with an item that is displayed in the ListView control.

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.


ListViewItem Collection Editor

When you select the first item on the ListView Tasks menu, the ListViewItem Collection Editor is displayed. This editor can also be accessed by clicking on the Image:ellipsesbutton.gif button in the right hand column of the Items property in the Properties Window.

Image:526-93.jpg


Once the editor is open, ListViewItems can be added or removed, and their properties can be set. A ListViewItem object can display an image along with text. An ImageList must be used to associate an image with a ListViewItem object.

The ListViewItem class represents items in a ListView control and the ListViewSubItem class represents a sub-item related to a ListViewItem. The ListViewItem class exposes the SubItems collection property that represents the list of sub-items related to an item. The number of sub-items should equal the number of columns minus one.

This editor can also be accessed by clicking on the Image:ellipsesbutton.gif button in the right hand column of the SubItems property in the ListViewItem Collection Editor.

Image:526-97.jpg


Adding items manually

Items can be added manually by adding ListViewItems to the ListView's Items collection property. The following code demonstrates how to add items to a ListView control:

ListViewItem listViewItem1 = new ListViewItem();
ListViewSubItem listViewSubItem1 = new ListViewSubItem();
 
listViewItem1.Text = "Joe";
listViewSubItem1.Text = "123 Somewhere Street";
 
listViewItem1.SubItems.Add(listViewSubItem1);
listView1.Items.Add(listViewItem1);


ColumnHeader Collection Editor

When you select the second item on the ListView Tasks menu, the ColumnHeader Collection Editor is displayed. This editor can also be accessed by clicking on the Image:ellipsesbutton.gif button in the right hand column of the Columns property in the Properties Window.

This editor allows you to view and change the list of columns for a ListView control.

Image:526-94.jpg

Once the editor is open, use the Add and Remove buttons to modify the list of ColumnHeader objects in the ListView control.


Adding columns manually

Columns can be added manually by adding ColumnHeader objects to the ListView's Columns collection property. The columns will only be displayed when the ListView is in Details mode. The following code demonstrates how to add columns to a ListView control:

ColumnHeader columnHeader1 = new ColumnHeader();
ColumnHeader columnHeader2 = new ColumnHeader();
ColumnHeader columnHeader3 = new ColumnHeader();
 
columnHeader1.Text = "Name";
columnHeader2.Text = "Address";
columnHeader3.Text = "Telephone Number";
 
listView1.Columns.Add(columnHeader1);
listView1.Columns.Add(columnHeader2);
listView1.Columns.Add(columnHeader3);

ListViewGroup Collection Editor

When you select the third item on the ListView Tasks menu, the ListViewGroup Collection Editor is displayed. This editor enables you to display related sets of items in groups.

Image:526-95.jpg

Once the editor is open, you can add group headings. These groups are separated on the screen by horizontal group headers that contain the group titles. Once a group has been added, an item can be associated with it.


Add groups manually

Groups can be added manually by adding ListViewGroup objects to the ListView's Groups collection property. The following code demonstrates how to add a group to a ListView control:

listView1.Groups.Add(new ListViewGroup("Friends", HorizontalAlignment.Left));


Setting the view

The fourth item on the ListView Tasks menu allows the view that will be shown at run time to be selected.

Image:526-96.jpg


Setting the view manually

The following code demonstrates how to set the view for a ListView control:

// Set the view to show details.
listView1.View = View.Details;


prevpp.png  nextpp.png
C# Online.NET