Context Menu question

J

J L

I have a form with a listview on it and a context menu. I handle the
right click event on the listview but after handling the event, the
context menu then appears. I would like to disable it if the user
right clicks on an item in the listview. I do like it to appear if the
user right clicks on an empty part of the listview.

Can this be done? If so how?

TIA
John
 
P

Pipo

Hi J L,

In mousedown event of the listview:

Dim blnState As Boolean = (e.Button = MouseButtons.Right) AndAlso
(ListView1.SelectedItems.Count > 0)

mnuAdd.Visible = blnstate

mnuRemove.Visible = blnstate
 
H

Herfried K. Wagner [MVP]

J L said:
I have a form with a listview on it and a context menu. I handle the
right click event on the listview but after handling the event, the
context menu then appears. I would like to disable it if the user
right clicks on an item in the listview. I do like it to appear if the
user right clicks on an empty part of the listview.

\\\
Private Sub ListView1_MouseUp( _
ByVal sender As Object, _
ByVal e As MouseEventArgs _
) Handles ListView1.MouseUp
Dim i As ListViewItem = Me.ListView1.GetItemAt(e.X, e.Y)
If i Is Nothing Then
Me.ContextMenu1.Show(...)
End If
End Sub
///
 
J

J L

Thank you both. Exactly what I needed.

John

\\\
Private Sub ListView1_MouseUp( _
ByVal sender As Object, _
ByVal e As MouseEventArgs _
) Handles ListView1.MouseUp
Dim i As ListViewItem = Me.ListView1.GetItemAt(e.X, e.Y)
If i Is Nothing Then
Me.ContextMenu1.Show(...)
End If
End Sub
///
 

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