ColumnClick, ItemClick Events (ListView)

Purpose

ColumnClick occurs when a ColumnHeader object in a ListView control is clicked. Only available in Report view (3).

ItemClick occurs when a ListItem object in a ListView control is clicked.

Syntax

Sub ListView_ColumnClick(ColumnHeader As ColumnHeader)

Sub ListView_ItemClick(Item As ListItem)

Description

The ColumnClick(ColumnHeader As ColumnHeader) event commonly use the Sort property to sort the ListItem objects in the clicked column.

Use the ItemClick(Item As ListItem) event to determine which ListItem was clicked. This event is triggered before the Click event. The standard Click event is generated  if the mouse is clicked on any part of the ListView control. The ItemClick event is generated only when the mouse is clicked on the text or image of a ListItem object.

Example

Ocx ListView lv1 = "", 10, 10, 200, 200

.View = 3 : .FullRowSelect = True

lv1.ColumnHeaders.Add  , , "Column1" : lv1.ColumnHeaders.Add , , "Column2"

lv1.Add , , "" : lv1.ListItem(1).AllText = "Bobby;Moore"

lv1.Add , , "" : lv1.ListItem(2).AllText = "Jack;Charlton"

lv1.Add , , "" : lv1.ListItem(3).AllText = "Bobby;Charlton"

Do : Sleep  : Until Me Is Nothing

 

Sub lv1_ItemClick(Item As ListItem)

Message(Item.SubItems(0) & " " & Item.SubItems(1))

EndSub

 

Sub lv1_ColumnClick(ColumnHeader As ColumnHeader)

Global lv1IsSorted As Int

Const lvwDescending = $10000

If ColumnHeader.Index == lv1IsSorted

lv1.Sort lv1IsSorted - 1, 1 + lvwDescending

lv1IsSorted = 0

Else

lv1IsSorted = ColumnHeader.Index

lv1.Sort lv1IsSorted - 1, 1

EndIf

End Sub

The ListView lv1 is sorted case-insensitive (compare% = 1) and the sort order is toggled when the column is clicked again.

To recognize a second click a global variable IsSorted is used (here lv1IsSorted to identify the control). The IsSorted variable holds the latest clicked column. When a column is clicked for a second time lv1IsSorted is equal to the Index of the column. But, of course the Tag property of the ColumnHeader item can also be used to store the current sort order.

See Also

ListView, ColumnHeader, ListItem, Sort

{Created by Sjouke Hamstra; Last updated: 25/09/2014 by James Gaite}