Each item in the
ListView is called a
ListItem object. If you look at Figure 4.6, for example, you will see a
list of files in the Visual Basic directory. Biblio.mdb, Readme.hlp, and each
of the other files in the right side of the Windows Explorer is a
ListItem.
The ListView control organizes all the
ListItem objects into a single collection called ListItems.
This is similar to a TreeView, which organizes
each Node object in a tree into the Nodes Collection. The
ListItems Collection can be used to cycle through all the objects in the
control for processing, just as with any other collection (see Listing 4.2).
LISTING 4.2
PROCESSING THE ITEMS CONTAINED IN
A LISTVIEW CONTROL
Dim objItem as ListItem
For Each objItem in ListView1.ListItems
' do some processing÷
Next
Index and Key Properties
You can refer to Items in a ListView by either the
Index property or the Key property. The
Index property is an integer expression typically generated by Visual Basic
when a ListItem is added to the ListView. You can refer
to a specific item by number:
Msgbox ListView1.ListItems(1).Text
Alternatively you can loop through the collection
of ListItems, referring to each by number, as
illustrated in Listing 4.3.
LISTING 4.3
LOOPING THROUGH A LISTVIEW CONTROL'S
LISTITEMS BY NUMBER
For I = 1 To ListView1.ListItems.Count
Msgbox ListView1.ListItems(I).Text
Next I
Unlike the Index in the TreeView, you have some
control over the value of the Index property in the
ListView control. Specifying an Index number is discussed later in this
section.
A more convenient way to reference an item in the list is by Key. The
Key property is a string expression—assigned by you as the developer
(or by the user if you desire)—that can also be used to access an item in
the list. The Key property is included as part of the
Add method (discussed later) when an item is inserted into the list.
As a developer you usually know the value for
the Key and can access a node directly. It is
easier to refer to the node you want by using
a meaningful text string than by using the Index
property that is determined by Visual Basic.
Unlike the TreeView you can store numbers in the
Key property of each ListItem if necessary.
You just have to convert the number to a string by using the
Str$() function, and the string value will be accepted as the Key by Visual
Basic.
ListView Control topics
.<< Previous | Contents
| Next >>