Is it possible to leave a ContextMenuStrip open after a selection/check of certain items?
I plan on using a simple ContextMenuStrip to set a filter (this way i could use the same filter either in a menu or as a right-click option).
The menu lists a number of items, and i would like the user to be able to make a selection of the items using the basic Check functionality. Once the selection is done the user can click an Activate filter option or can click outside the menu to either activate or cancel the filter.
On a selection/click event the menu normally closes. Is it possible to keep the menu open on a click event?
What i found strange is that
ContextMenuStrip.Closing
event fires before theToolStripMenuItem.Click
event. The solution was to useContextMenuStrip.ItemClicked
event where you havee.ClickedItem
, and then check if it's one of the items that, when clicked, won't close theContextMenuStrip
, and set the appropriate flag. Then inContextMenuStrip.Closing
you can sete.Cancel = true;
if the flag is also set. Don't forget to reset the flag though.I found this useful for my purposes.
You could use
ItemClicked
to read the tag or some other property.I just wanted a simple item that made clear to the user which item the context menu was going to effect.
I don't think there is a property for this in the ContextMenuStrip.
The workaround we use in our application is that on the clicked event of the ContextMenuStrip, we do some processing, then if we want the context menu to stay open we simply call ContextMenuStrip.Show again.
This will work well if there is only one level to the ContextMenuStrip. If there are sub-menus and sub-sub-menus, then you would have to re-select the menus that were open before the click and I'm not sure how that can be done...
To prevent the contextmenu from closing when an item is clicked, do the following.
On mousedown event of ContextMenuItems set flag to false then set it back to true at the closing event of the contextmenu.
Example:
The best way I found to do this without flickering is to use the MouseDown and MouseLeave events for every button in the DropDown menu.
Example:
This is my method; it's flicker-free and - I think - a bit more flexible.
If you have a set of ToolStripMenuItems you'd like to use as toggle buttons (option on/off), try this:
(The
ctxWildCards
is just myContextMenuStrip
, used to select filters based on file types - for search or FileDialogs)This is in Visual Basic (obviously! ;), so you can add Handlers programmatically or using 'Handles...' clauses.