Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering


You are here: Visual Basic > Advanced VB6 tutorial > Chapter 4

SubItems Property, ItemClick Event and ColumnClick Event of ListView Control in Visual Basic 6

SubItems Property

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:

Msgbox ListView1.ListItems(1).SubItems(2)

For this example the above code will display the text that appears in the third column, SubItems(2), of the first ListItem, ListItems(1).

ItemClick Event

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.

ColumnClick Event

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 4.4.

LISTING 4.4

MATCHING A COLUMNHEADER WITH ITS CORRESPONDING SORTKEY

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
'MsgBox ColumnHeader.Index
ListView1.SortKey = ColumnHeader.Index - 1
ListView1.Sorted = True
End Sub

ListView Control topics

See Also


  

<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved