Search code examples
vb.netlistviewcontextmenustrip

Context Menu Strip Control With and Without Selected Grid


I really don't know what to search so I'll just ask this:

I have a context menu strip linked to a listview,

what I need is that when I right click to a highlighted variable in the grid list view, the menu for items will be displayed, and when I right click to an empty grid, the grid settings will be displayed.

How can I achieve this?


Solution

  • I doubt the user will appreciate your approach; you plan to display 2 different menus from the same action - a click. It may not be apparent how to get one menu or the other. Further, it is not clear why the "Settings" or main menu would ever need to be hidden from them.

    Rather than 2 menus, why not 1 menu with 2 submenus, to make it easy to find them?

    Private Sub myLV_MouseUp(sender...
        If (e.Button = Windows.Forms.MouseButtons.Right) Then
    
            lvCMS.Items("ItemMenu").Enabled = (myLV.SelectedItems.Count > 0)
    
            ' show (may need if there ARE 2 menus)
            lvCMS.Show(myLV.PointToScreen(New Point(e.X, e.Y)))
    
        End If
    End Sub
    

    enter image description here

    The LV will show an assigned menu on its own, but if you have 2, you may want/need to display it manually, which is shown. By using the MouseUp event the LV has already reacted to MouseDn and changed the selected item. The only thing that varies menu-wise is whether the ItemMenu is enabled or not.

    Clicking on an empty area deselects an item. Barring a button to do the same, it is hard to deselect an item because the empty area can get be almost nonexistent:

    enter image description here

    Only the orange-ish area is empty grid, add one more item to fill in the bottom and cause a scroll bar and there is none. This is partly why a separate menu and special set of conditions may not be a good idea.