SubItemIndex Property

Purpose

Returns an integer representing the index of the sub item associated with a ColumnHeader object in a ListView control.

Syntax

ColumnHeader.SubItemIndex [ = index%]

Description

Subitems are arrays of strings representing the ListItem object's data when displayed in Report view.

The SubItemIndex is used to associate the SubItems string with the correct ColumnHeader object.

The first column header always has a SubItemIndex property set to 0 because the small icon and the ListItem object's text always appear in the first column and are considered ListItem objects rather than subitems.

The number of column headers dictates the number of subitems. There is always exactly one more column header than there are subitems.

Example

Global a$, n As Int32

Ocx ListView lv = "", 10, 10, 400, 200 : .View = 3 : .FullRowSelect = True : .GridLines = True

For n = 1 To 4 : lv.ColumnHeaders.Add , , "Column" & n : lv.ColumnHeaders(n).Alignment = 2 : Trace lv.ColumnHeaders(n).SubItemIndex : Next n

For n = 1 To 4

lv.Add , , ""

a$ = Rand(10) & ";" & Rand(10) & ";" & Rand(10) & ";" & Rand(10)

lv(n).AllText = a$

Next n

Do : Sleep : Until Me Is Nothing

 

Sub lv_ColumnClick(ColumnHeader As ColumnHeader)

// ColumnHeader.SubItemIndex returns 0; use ColumnHeader.Index - 1 instead

Local chi As Int = ColumnHeader.Index - 1, li As ListItem

If lv.SelectedCount <> 0

Set li = lv.SelectedItem

Message "Contents of Column" & (chi + 1) & " and Row" & li.Index & #13#10 & "is " & li.SubItems(chi)

Else

Message "Select a row first"

EndIf

EndSub

Known Issues

As shown in the example above, SubItemIndex appears to return 0 regardless of the ColumnHeader selected. As a workaround, use ColumnHeader.Index - 1 instead; this will work as long as the ColumnHeaders appear in the order they were first added to the listview.

See Also

ColumnHeader, ListView

{Created by Sjouke Hamstra; Last updated: 23/10/2014 by James Gaite}