help on contextmenustrip

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
 
L

Lloyd Sheen

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
 
C

Co

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
 
L

Lloyd Sheen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top