help on contextmenustrip

  • Thread starter Thread starter Co
  • Start date Start date
C

Co

Hi All,

I created a contextmenustrip which is connected to a listview.
Now I need some way to check if I right-clicked on a listview item
before calling the msgbox.
Right now I can click wherever on the listview it will always fire, if
there's an item or not.

Private Sub OpenToolStripMenuItem1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
OpenToolStripMenuItem1.Click
MsgBox("Open")
End Sub

Private Sub EditToolStripMenuItem1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
EditToolStripMenuItem1.Click
MsgBox("Edit")
End Sub
Private Sub DeleteToolStripMenuItem1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
DeleteToolStripMenuItem1.Click
MsgBox("Delete")
End Sub

Regards
Marco
 
Co said:
Hi All,

I created a contextmenustrip which is connected to a listview.
Now I need some way to check if I right-clicked on a listview item
before calling the msgbox.
Right now I can click wherever on the listview it will always fire, if
there's an item or not.

Private Sub OpenToolStripMenuItem1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
OpenToolStripMenuItem1.Click
MsgBox("Open")
End Sub

Private Sub EditToolStripMenuItem1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
EditToolStripMenuItem1.Click
MsgBox("Edit")
End Sub
Private Sub DeleteToolStripMenuItem1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
DeleteToolStripMenuItem1.Click
MsgBox("Delete")
End Sub

Regards
Marco

What you are looking for is HitTest. This will return a ListViewHitTestInfo
object which will provide the listviewitem if you right click over an item.

LS
 
What you are looking for is HitTest.  This will return a ListViewHitTestInfo
object which will provide the listviewitem if you right click over an item.

LS- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -

Cheers Lloyd,

Could you give me a small example please.

Marco
 
What you are looking for is HitTest. This will return a
ListViewHitTestInfo
object which will provide the listviewitem if you right click over an
item.

LS- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -

Cheers Lloyd,

Could you give me a small example please.

Marco


Marco,

There are several steps to accomplish this.

1. On the MouseDown event for the ListView capture and store the x and y
cooridinates of the mouse pointer as:

Private _MouseX As Integer
Private _MouseY As Integer

Code in the event handler
_MouseX = e.X
_MouseY = e.Y

2. In the Opening event of the context menu

Dim mousePos as new Point(_MouseX,_MouseY)
Dim tstOBj As ListViewHitTestInfo = Me.SongFileView.HitTest(mousePos)

Now you can test the properties of tstOBj to see if there is a listviewitem.

Hope this helps
LS
 
Back
Top