ListView
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| Exam 70-526 Preparation Guide: Configure the layout and functionality of a Windows Form to display a list of items |
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.
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.
Inheritance hierarchy
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ListView
Useful properties
-
Activation- Defines the type of action the user must take to activate an item. Takes a value from theItemActivationenumeration:
-
-
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 theViewproperty is set toLargeIconorSmallIcon. Takes a value from theListViewAlignmentenumeration:
-
-
Default- When the user moves an item, it remains where it is dropped. -
Left- Items are aligned to the left of theListViewcontrol. -
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 theListViewcontrol. (Default setting)
-
-
AllowColumnReorder- Indicates whether the user can drag column headers to reorder columns in the control at run time when theViewproperty is set toDetails.
-
-
AutoArrange- Determines whether icons are automatically kept arranged and snapped to the grid when theViewproperty is set toLargeIconorSmallIcon.
-
-
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 theViewproperty is set toDetails.
-
Note: - If your
ListViewcontrol does not have any column headers specified and you set theViewproperty toDetails, theListViewcontrol will not display any items.
- If your
-
ContextMenuStrip- Defines the ContextMenuStrip associated with this control.
-
-
FullRowSelect- Indicates whether clicking an item selects all its sub-items when theViewproperty is set toDetails.
-
-
Groups- Gets the collection ofListViewGroupobjects assigned to the control. TheListViewgrouping feature lets you create visual groups of logically relatedListViewitems. 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 theViewproperty of theListViewcontrol is set toDetailsand theListViewcontrol hasColumnHeaderobjects specified in theListView.ColumnHeaderCollection. Takes a value from theColumnHeaderStyleenumeration:
-
-
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
Activationproperty to be set toOneClickand theHoverSelectionproperty to be set to true. In addition, run-time label editing is not allowed when hot tracking is enabled.
- Enabling hot tracking will cause the
-
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 theListViewcontrol to select the item. Multiple items can be selected (when theMultiSelectproperty is set to true) by holding down the control key while pointing to each item.
-
-
InsertionMark- Gets aListViewInsertionMarkobject used to indicate the expected drop location when an item is dragged within aListViewcontrol. 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 theAutoArrangeproperty is set to true and when theListViewcontrol does not sort the items automatically.
-
-
Items- Gets a collection containing all of theListViewItemsin 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 theActivationproperty is set toOneClickorTwoClick, 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 theListViewItemobjects contained in theListView.
-
-
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 theViewenumeration:
-
-
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 theListViewcontrol displays in theLargeIconview.
-
ListView Tasks
When you select the ListView and click on the Smart Tag icon
, a small menu appears with the list of tasks that can be achieved through it.
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
button in the right hand column of the Items property in the Properties Window.
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
button in the right hand column of the SubItems property in the ListViewItem Collection Editor.
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
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.
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 only 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.
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.
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;
MSDN references
|








