Each item in the ListView control may also have 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 the Report 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 on an icon, show the values of the selected item’s subitems on other controls, or simply access them from within your code.
To access the subitems of a given item, use its SubItems collection. The first sub- , item is Subltem(l), the second one. is Subltems(2), and so on. The following statements add an item and three subitems to the ListViewl control:
To access the Subltems collection, you must have a reference tJ 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. Subltems is ,\ property of the Listltem object, which is not a collection. It’s an array of strings that holds the values of the subitems.
Displaying the subitems on the control requires some overhead. ~lIbitems are displayed only in the Report view mode. However, setting the View propcrtv tD IvwReport is not enough. You must first create the headers of the Report view.as shown. The ListView control will display only as many subitcms as there are headers 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 subitem will be displayed under each header, The first subitem (“Maria Anders” in the above example) will be displayed under the second header, the second subitem (“030-0074321” in the same example) ill 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).
The simplest way to define the headers is through the Column Headers tab of the control’s property pages. To add a column, dick Insert Column and then specify the header’s properties in the text fields: the header’s text, its alignment, width. key, and tag. There’s no reason to index the columns, except that it’s easier to refer to them by a name.(their key), than by their index, which is a numeric value.
You can also insert columns and header from within your code. The headers of a List.View control can be accessed through another collection, the ColumnHeaders collection. The ColumnHeaders collection. exposes the standard members of a collection. Here’s the syntax of its Add method:
‘
The alternate form of its syntax is:
CHeaderl = ColumnHeaders.Add(Index, Key, Text, Width, Alignment, Icon)
The arguments of the Add method are the same ones you can specify through ·theColumn Headers property page. As with the Listltemscollection’s Add method, all arguments are optional. Usually the text, an icon, and the column’s width are, specified.:
Unless yo~ set up each column’s width, all columns will have the same width. The width of individual columns ~ usually specified as a percentage of the total width of the control. The following code sets up a ListView control with four headers, all having the same width:
This subroutine sets up four headers and then sets the control’s View property to IuwReport. The first header corresponds to the item (not a subitem). The number of headers you set up must be equal to the number of sub items you want to display on the control plus one.
VB6 at Work: the ListViewDemo Project
Let’s put together the members of the ListView control to create a sample application that populates a ListView control and enumerates its items. The application is called ListViewDePlo and you’ll find it in the LVWDemo folder under this Chapter’s folder on the CD. The application’s Form, shown , contains a ListView control who SP items can be displayed in all possible views, depending on the status of the Option Button controls in the List Style ‘section on the right side of the Form.
When the application starts, it sets up the headers (columns) of the Listview control. The headers are displayed only in the Report view, but they are required’ for the list’s subitems. Unless you create the appropriate headers, you won’t be able to’ add subitems. You can comment out the lines that insert the headers in the Form’s Load event and then run the project to see what happens when Visual Basic runs . , into a statement that attempts to add subitems.
Let’s start by looking at the code of the Form’s Load event. In this event, we set up the control’s headers. All columns have the same width, but you can adjust their widths according to the data to be displayed. The ListView control’s headers can also be resized by the user at runtime .
To populate the ListVlew control click the Populate List button, whose code is shown next. To add an item. the code calls the control’s Add method, which returns a ListItem variable. This valuable is used to append the item’s subitems With the Sub- Items property. Notice that SubItems doesn’t provide an Add method. It’s an array with as many elements as there are columns in the control, and you Om assign values to its elements.
It Shows the statements that insert the first two’ items in the list. The
remaining items added with similar statements, which need not be repeated here. The sample data I used in the ListViewDemo application came from-the Cmd sample database, which comes with Visual Basic and is installed iJl the . . Visual Basic 6 folder.
Enumerating the List
The Enumerate List button scans all the items in the list and displays them along with their subitems on the Immediate window. To.scan the list, you must set up a loop that enumerates all the items in the ListItems collection. To reach item in the . list, set up a nested loop that scans all the subitems of the current item. The complete listing of the Enumerate List button is shown next.
The output of this code on the Immediate window is shown. The subitems appear under the corresponding item and they arc indented by five , spaces.
Code uses the ListView control’s Subltems property to access the current item’s subitems. An alternate method for accessing the same subitem is to assign .. the current item to an item variable, Lliem, and then use this object’s ListSubItems property, which is a collection that contains the current item’s subitems. The following subroutine does the exact same thing as Code 8.19, only it uses the List-· Sub Items collection.