The Items Collection
All the items on the ListView control form a collection: the Items collection. This collection exposes the typical members of a collection that let you manipulate the control’s items. These members are discussed next.
Add method – This method adds a new item to the Items collection. The syntax of the Add method is as follows:
ListView1.Items.Add(caption)Code language: VB.NET (vbnet)
You can also specify the index of the image to be used, along with the item and a collection of subitems to be appended to the new item, by using the following form of the Add method:
ListView1.Items.Add(caption, imageIndex)Code language: VB.NET (vbnet)
where imageIndex is the index of the desired image on the associated ImageList control.
Finally, you can create a ListViewItem object in your code and then add it to the ListView control by using the following form of the Add method:
ListView1.Items.Add(listItemObj)Code language: VB.NET (vbnet)
The following statements create a new item, set its individual subitems, and then add the newly created ListViewItem object to the control:
LItem.Text = "new item" LItem.SubItems.Add("sub item 1a") LItem.SubItems.Add("sub item 1b") LItem.SubItems.Add("sub item 1c") ListView1.Items.Add(LItem)Code language: VB.NET (vbnet)
Count property – Returns the number of items in the collection.
Item property – Retrieves an item specified by an index value.
Clear method – Removes all the items from the collection.
Remove method – Removes an item from the collection.
The SubItems Collection
Each item in the ListView control may have one or more subitems. You can think of the item as the key of a record, and the subitems as the other fields of the record. The subitems are displayed only in Details mode, but they are available to your code in any view. For example, you can display all items as icons, and when the user clicks an icon, show the values of the selected item’s subitems on other controls.
To access the subitems of a given item, use its SubItems collection. The following statements add an item and three subitems to the ListView1 control:
Dim LItem As ListViewItem LItem = ListView1.Items.Add("Alfred's Futterkiste") LItem.SubItems.Add("Maria Anders") LItem.SubItems.Add("030-0074321") LItem.SubItems.Add("030-0076545")Code language: VB.NET (vbnet)
To access the SubItems collection, you need a reference to the item to which the subitems belong. The Add method returns a reference to the newly added item, the LItem variable, which is then used to access the item’s subitems, as shown in the preceding code segment. Displaying the subitems on the control requires some overhead. Subitems are displayed only in Details view mode. However, setting the View property to Details is not enough. You must first create the columns of the Details view, as explained earlier. The ListView control displays only as many subitems as there are columns in the control. The first column, with the header Company, displays the items of the list. The following columns display the subitems. Moreover, you can’t specify which subitemwill be displayed under each header. The first subitem (Maria Anders in the preceding example) will be displayed under the second header, the second subitem (030-0074321 in the same example) will be displayed under the third header, and so on. At runtime, the user can rearrange the columns by dragging them with the mouse. To disable the rearrangement of the columns at runtime, set the control’s AllowColumnReorder property to False (its default value is True).
Unless you set up each column’s width, they will all have the same width. The width of individual columns is specified in pixels, and you can set it to a percentage of the total width of the control, especially if the control is docked to the form. The following code sets up a ListView control with four headers, all having the same width:
Dim LWidth As Integer LWidth = ListView1.Width - 5 ListView1.ColumnHeaders.Add("Company", LWidth / 4) ListView1.ColumnHeaders.Add("Contact", LWidth / 4) ListView1.ColumnHeaders.Add("Phone", LWidth / 4) ListView1.ColumnHeaders.Add("FAX", LWidth / 4) ListView1.View = DetailsViewCode language: VB.NET (vbnet)
This subroutine sets up four headers of equal width. The first header corresponds to the item (not a subitem). The number of headers you set up must be equal to the number of subitems you want to display on the control, plus one. The constant 5 is subtracted to compensate for the width of the column separators. If the control is anchored to the vertical edges of the form, you must execute these statements from within the form’s Resize event handler, so that the columns are resized automatically as the control is resized.