Retrieving SubItem Index on click event in ListView

andycee

Member
Joined
Mar 24, 2006
Messages
11
Programming Experience
1-3
Couldn't see how VB.NET allows you to get the SubItem Index (column index) following a click event in a ListView.

Used the following function to resolve this issue :

Private Function GetSubItemIndex(ByVal x As Integer, ByVal lv As ListView) As Integer
Dim c As Integer = 0
Dim col As ColumnHeader
For Each col In lv.Columns
c = c + col.Width
If x < c Then Exit For
Next
If x > c Then
GetSubItemIndex = -1
Else
GetSubItemIndex = col.Index
End If
End Function


Hope this helps anyone !
 
Yes, that is not best the solution, use HitTest method instead (new in .Net 2.0):
VB.NET:
Private Sub ListView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles ListView1.MouseUp
  Dim hti As ListViewHitTestInfo = ListView1.HitTest(e.Location)
  Dim six As Integer = hti.Item.SubItems.IndexOf(hti.SubItem)
  MsgBox(six.ToString)
End Sub
Moved to Listviews forum.
 
Actually Flash was introduced in 1996, you don't think HitTest methods existed before that? Anyway, good the Listview got it too. .Net 2.0 is great progress!
 
We are nearly loosing focus on the Listview SubItem HitTest here.. :) In my narrowMindednes$ I was thinking more about hittesting in the first graphical operating systems with mouse input devices like Mac (1984) and Windows (1985) than Java (1990) and Flash (1996). Macromedias new partners Adobe with "more than two decades" at it is also probably more in the vein of early hittesting.
 
Back
Top