Public Class Form1.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With Me.ListView1
'Setup listview properties
.View = View.Details
.Dock = DockStyle.Fill
.GridLines = True
.FullRowSelect = True
'Add columns
For i As Integer = 0 To 5
.Columns.Add("Col " & i)
Next
'Add rows
For y As Integer = 0 To 15
Dim objLvi As New ListViewItem
objLvi.Text = "Cell " & "0" & "," & y
For x As Integer = 1 To 5
objLvi.SubItems.Add("Cell " & x & "," & y)
Next
.Items.Add(objLvi)
Next
End With
End Sub
Private Sub ListView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseClick
Dim col As Integer = getClickedColumn(Me.ListView1, e.X)
Dim row As Integer = Me.ListView1.SelectedIndices(0)
Dim cellText As String = Me.ListView1.Items.Item(row).SubItems.Item(col).Text
MsgBox("col " & col & ", row " & row & _
Environment.NewLine & _
"text = " & Chr(34) & cellText & Chr(34))
End Sub
Private Function getClickedColumn(ByVal pListView As ListView, ByVal pMouseX As Integer) As Integer
Dim result As Integer = 0
'Get column rights
Dim colRights As New List(Of Integer)
Dim colWidths As New List(Of Integer)
For Each col As ColumnHeader In pListView.Columns
colWidths.Add(col.Width)
Dim colRight As Integer = 0 ' - pListView.Columns.Item(0).Width 'Subtract this if you were collecting lefts instead of rights
For i As Integer = 0 To colWidths.Count - 1
colRight += colWidths(i)
Next
colRights.Add(colRight)
Next
'Which column does the mouse X fall inside?
Dim colIndex As Integer = 0
For Each colRight As Integer In colRights
If pMouseX <= colRight Then
result = colIndex
Exit For
End If
colIndex += 1
Next
Return result
End Function
End Class