ContextMenuStrip On RightClick, IF Items Are Selected In Listview?

sooprise picture sooprise · Aug 24, 2010 · Viewed 9.9k times · Source

I have a ContextMenuStrip attached to a list view, and it's working great, but what I'm interested in knowing is how I can have it show up only when one or more items are selected in the listview.

Thanks!

Answer

Fredrik Mörk picture Fredrik Mörk · Aug 24, 2010

You could use the Opening event. The event args has a Cancel property so that you can examine the state of your application and decide whether to have the menu show (by doing nothing) or prevent it from showing (by setting e.Cancel = true). However, like @Grzenio mentions, I would find it more intuitive if the item that i right-clicked on became selected automatically.

Another option would be to use the Opening event to populate the context menu with only one disabled item, with a text like (no item is selected) or so; this would inform the user about why the command is not available.