Disabling Items on a Context Menu in a Listview

C

Charles May

I have a listview that I want to be able to right-click on an item and
delete that row.

I can do this easily, however, I want to only enable the delete menu option
if the mouse pointer is over an actual populated row. Right now, if I right
click in the lower (unpopulated) half of the listview, I can still click
delete. Which seems to automatically delete the first item in the list.

any help is appreciated

Thanks

Charlie
 
P

Phill W.

Charles,

For the benefit of the many, /many/ developers who will undoubtedly
follow in your footsteps ... how???

Regards,
Phill W.
 
C

Charles May

OK, this is what I came up with.
Private Sub lvwNotifications_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lvwNotifications.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
Try
Dim x As Integer = lvwNotifications.GetItemAt(e.X, e.Y).Index
If x >= 0 Then
'the menuitems to enable if the mouse is over a populated row.
Me.mnuDelete.Enabled = True
Me.mnuToggle.Enabled = True
Me.mnuEdit.Enabled = True
End If
Catch ex As Exception
'if not a populated row, disable these menuitems.
Me.mnuDelete.Enabled = False
Me.mnuToggle.Enabled = False
Me.mnuEdit.Enabled = False
End Try
End If

End Sub

Charlie
 

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

Similar Threads


Top