We help IT Professionals succeed at work.

VB.net Get row and column indexes of ListView on right click

Murray Brown
Murray Brown asked
on
How do I get the row and column index of a ListView on right click?
Comment
Watch Question

Most Valuable Expert 2012
Top Expert 2008

Commented:
Start with the ListView.HitTest method, which returns a ListViewHitTestInfo instance.  Then, get the ListViewHitTestInfo.Item, and SubItem.

Item.Index should be row and SubItem.Index should be column.
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
The HitTest was the easy part
Getting the rest is what I am after
Most Valuable Expert 2012
Top Expert 2008
Commented:
Dim rowIndex As Integer = ListViewHitTestInfo.Item.Index
Dim columnIndex As Integer = ListViewHitTestInfo.SubItem.Index
it_saigeDeveloper
Distinguished Expert 2019
Commented:
Just a small example of what Bob is telling you:

Form1.vb -
Public Class Form1

	Private Sub OnMouseClick(ByVal sender As Object, ByVal e As MouseEventArgs) Handles ListView1.MouseClick
		Dim lv As ListView = DirectCast(sender, ListView)
		If e.Button.Equals(MouseButtons.Right) Then
			Dim cm As New ContextMenu()
			For Each v As View In [Enum].GetValues(GetType(View))
				If lv.View.Equals(v) Then
					cm.MenuItems.Add(New MenuItem(v.ToString(), AddressOf OnClick) With {.Checked = True, .Tag = v})
				Else
					cm.MenuItems.Add(New MenuItem(v.ToString(), AddressOf OnClick) With {.Tag = v})
				End If
			Next

			Dim currentItem = lv.HitTest(e.X, e.Y).Item
			If currentItem IsNot Nothing Then
				cm.MenuItems.Add(New MenuItem("-"))
				cm.MenuItems.Add(New MenuItem(String.Format("Do something to {0} located at ({1}, {2})", currentItem, e.X, e.Y), AddressOf OnClick) With {.Tag = currentItem})
			End If
			cm.Show(lv, New Point(e.X, e.Y))
		End If
	End Sub

	Private Sub OnClick(ByVal sender As Object, ByVal e As EventArgs)
		If TypeOf sender Is MenuItem Then
			Dim mi As MenuItem = DirectCast(sender, MenuItem)
			If TypeOf sender.Parent Is ContextMenu Then
				Dim cm As ContextMenu = DirectCast(sender.Parent, ContextMenu)
				If mi.Tag IsNot Nothing Then
					If TypeOf mi.Tag Is View AndAlso TypeOf cm.SourceControl Is ListView Then
						Dim lv As ListView = DirectCast(cm.SourceControl, ListView)
						Dim v As View = DirectCast(mi.Tag, View)

						For Each item In cm.MenuItems
							item.Checked = False
						Next

						lv.View = v
						mi.Checked = True
					Else
						MessageBox.Show(String.Format("You selected {0}", mi.Text))
					End If
				End If
			End If
		End If
	End Sub

	Private Sub OnLoad(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
		ListView1.Items.Add(New ListViewItem("Cat"))
		ListView1.Items.Add(New ListViewItem("Dog"))
		ListView1.Items.Add(New ListViewItem("Hamster"))
		ListView1.Items.Add(New ListViewItem("Llama"))
	End Sub
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.ListView1 = New System.Windows.Forms.ListView()
		Me.SuspendLayout()
		'
		'ListView1
		'
		Me.ListView1.Location = New System.Drawing.Point(13, 13)
		Me.ListView1.Name = "ListView1"
		Me.ListView1.Size = New System.Drawing.Size(259, 237)
		Me.ListView1.TabIndex = 0
		Me.ListView1.UseCompatibleStateImageBehavior = False
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 262)
		Me.Controls.Add(Me.ListView1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ResumeLayout(False)

	End Sub
	Friend WithEvents ListView1 As System.Windows.Forms.ListView

End Class

Open in new window

Produces the following output -Capture.JPG
-saige-
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Thank you both very much