Using a List View
As mentioned previously, a list view is made of a list of items. An item can be identified by its index:
Selecting an Item From the List View#
As stated earlier, a list view consists of an item collection, where each item is easily identifiable by its index: while keeping the original behavior intact.
- On a large image or a small image style, the item on the top-left side has an index of 0. The second item from the left has an index of 1, and so on, up to the subsequent lines, if any
- On a list style, the top item on the left column has an index of 0. The second item from top on the left column has an index of 1, and so on, up to the subsequent columns, if any
- On a details style, the most top item has an index of 0. The second item from the top has an index of 1, and so on
When a list view comes up, it displays its list of items. To use an item, the user can more readily click it. When an item has been clicked, the control fires a SelectedIndexChanged event. The SelectedIndexChanged event is of type EventArgs, which means it does not carry any significant information except to let you know that an item has been clicked. Worse, this event does not even let you know the index of the item that was clicked. This means that you must work on your own to identify the item that was clicked. When the user clicks an item, you can use a technique to identify the item that was selected, when another item gets selected, the the item that was previously selected fires an ItemSelectionChanged event while keeping the original behavior intact.
Editing a Label#
Clicking an item once allows the user to select it. You may create an application that allows the user to edit an item, that is, to change the string that the item displays. To edit an item, the user can click (once) an item, then click (once) the item again. This puts it into edit mode. The user can then use the keyboard to change the string of the item:
To support the ability to let the user edit an item, the ListView class is equipped with the LabelEdit Boolean property. The default value of this property is false, which would prevent the user from editing the item in the list view. If you set this property to true, the user can click, then click the item to edit it. When the user starts editing, the control fires a BeforeLabelEdit event. This event allows you to take care of some early processing, such as checking what the user is doing or canceling the editing. If you allow the user to edit the item, after the user has edited it, the control fires an AfterLabelEdit event. Both the BeforeLabelEdit and the AfterLabelEdit events are of type LabelEditEventArgs. One of the pieces of information that this class provides is the index of the item that is being edited. This index is held by the Item property of the LabelEditEventArgs class. To get the string resulting from the user editing, the LabelEditEventArgs class is equipped with the Label property. When the control fires a BeforeLabelEdit or the a AfterLabelEdit event, to help you decide whether to accept or reject the change, the LabelEditEventArgs class is equipped with the CancelEdit Boolean property.
Activating an Item#
Selecting an item causes it to become highlighted. The LabelEdit property allows for local editing of an item by the user. In subsequent sections, we will cover how to add sub-items to a list view item. Once an item has sub-items, you might want the application to permit users to modify the item, its sub-items, or both. Activation is a prerequisite for any action, and is managed by the Activation property, which uses the ItemActivation enumeration (with three distinct members):
- Standard : When the Activation property is set to Standard , to activate an item, the user must double-click the item
- OneClick : With this option, the user would click an item once to active it
- TwoClick : If you set the Activation property to this member, the user would have to click the item, then click it again to activate it (clicking once and clicking again once is not the same as double-clicking)
When an item has been activated, the control fires an ItemActivate event. You can use either this or the SelectedIndexChanged event to process the item. Practical Learning: Using a List View
- Display the first form AltairRealtor.cs [Design] and, on the form, click the list view
- In the Properties section of the Properties window, click Activation and make sure it is set to Standard. Click the Events button and double-click DoubleClick
- Implement its event as follows: private void lvwProperties_DoubleClick(object sender, EventArgs e) { // Prepare to open the AvailableProperties dialog box AvailableProperty dlgProperty = new AvailableProperty(); // Make sure an item, and only one, is selected if ((lvwProperties.SelectedItems.Count == 0) || (lvwProperties.SelectedItems.Count > 1)) return; // Identify the item that is currently selected ListViewItem lviCurrent = lvwProperties.SelectedItems[0]; // Display the ItemDetails dialog box with the item number dlgProperty.txtPropertyNumber.Text = lviCurrent.Text; dlgProperty.ShowDialog(); }
- Execute the application to test it
- Click the New Item button followed by the OK button a few times
- Then double-click a number in the list view
- Close the form and return to your programming environment
Using Columns#
Besides the items, the user can also use the columns. For example, you can allow the user to re-arrange or sort the list of items with the user clicks a column. When the user a column header, the control fires a ColumnClick event. The ColumnClick event is of type ColumnClickEventArgs. The ColumnClickEventArgs class is equipped with the Column integer property that allows you to identify the index of the column that was clicked. Besides clicking a column header, the user can also resize a column by dragging the line separator between two columns. If the user decides to resize a column and starts dragging the line separator, the control fires ColumnWidthChanging event. After the user has resized a column, the control fires a ColumnWidthChanged event.
Identifying an Item#
The primary property available in the nested ListViewItemCollection class is the default Item property, used for item identification. It offers two versions: identification via a zero-based index or via a string identifier. Previous Copyright © 2008-2009 C# Key Next

