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

How do I get the row and column index of a ListView on right click?
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
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 BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
The HitTest was the easy part
Getting the rest is what I am after
Bob LearnedCommented:
Dim rowIndex As Integer = ListViewHitTestInfo.Item.Index
Dim columnIndex As Integer = ListViewHitTestInfo.SubItem.Index

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
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 BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thank you both very much
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.