You are here: Visual
Basic > Advanced VB6 tutorial
> Chapter 4
SubItems Property, ItemClick Event and ColumnClick Event of ListView Control
in Visual Basic 6
Once additional columns have been added to a ListView, those columns can be
accessed through the SubItems property of a ListItem. The text that appears in
a column for a particular ListItem can be read or set using:
For this example the above code will display
the text that appears in the third column, SubItems(2),
of the first ListItem, ListItems(1).
Most code associated with a ListView control
appears in either the ItemClick event or the
ColumnClick event. The ItemClick event occurs
when the user clicks on a ListItem within the
ListView control. The
ListItem that was clicked will be passed into
the event as an argument.
The ItemClick event occurs only when an item
in the list is clicked. If the user clicks anywhere
in the ListView control other than on an item,
the regular Click event is fired.
The ColumnClick event is fired when the user
clicks on the column header of the ListView.
The ColumnHeader object that was clicked is
passed into the event as an argument.
The code that is typically placed in the ColumnClick
event is the code to sort the ListItems by that
column. This is the normal behavior expected
by users in the Windows environment.
The ColumnHeaders is one-based, meaning that
the first column in the ColumnHeaders collection
has an index of 1. The SortKey property, however,
uses 0 as the index for the first key. Therefore
if you want to match up a ColumnHeader with
its corresponding SortKey, you must subtract
1 from the ColumnHeader index, as in Listing
MATCHING A COLUMNHEADER WITH ITS CORRESPONDING
Private Sub ListView1_ColumnClick _
(ByVal ColumnHeader As ComctlLib.ColumnHeader)
'Change the SortKey of the ListView
'to correspond to the SubItem in the just-clicked ColumnHeader
ListView1.SortKey = ColumnHeader.Index - 1
ListView1.Sorted = True
ListView Control topics
<< Previous | Contents
| Next >>