ContextMenuStrip In RightClick IF elements selected in the list?

I have a ContextMenuStrip related to the presentation of the list and it works fine, but I am interested to know how I can display it only when one or more elements are displayed in the list.

Thank!

+3
source share
4 answers

You can use the event Opening. The args event has a Cancel property so that you can check the status of your application and decide whether to show the menu (doing nothing) or not show it (by installation e.Cancel = true). However, as @Grzenio mentions, I would find it more intuitive if the element that I right-clicked was automatically selected.

Another option is to use the event Openingto populate the context menu with only one disabled item with text of type (no item is selected)or so; this will inform the user why the command is unavailable.

+4
source

For other people reading this topic, a good way is to omit the options in the menu (in the opening event) when no items are selected, and not display the menu at all

if (List.SelectedItems.Count == 0)
{
    // e.Cancel=true;
    List.Enabled = false;
}
else
{
    List.Enabled = true;
}
+3

, ( ), .

, , ContextMenuStrip, , , .

+2
   Private Sub ListView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
        If e.Button = MouseButtons.Right And ListView1.SelectedItems.Count > 0 Then
            Dim cn As New ContextMenuStrip()
            cn.Items.Add("Apple")
            Me.ListView1.ContextMenuStrip = cn
            cn.Show(Control.MousePosition.X, Control.MousePosition.Y)
        End If
    End Sub
+1

Source: https://habr.com/ru/post/1761327/


All Articles