Do not close ContextMenuStrip on selection of certain items

barry picture barry · May 15, 2009 · Viewed 13.8k times · Source

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?

Answer

Delirious picture Delirious · May 19, 2012

In case future programers are wondering how to do this, this is what I figured out. This will not close the context menu if any item is clicked. Create the context menu strip closing event and setup an if statement to cancel the close event if close reason is itemclicked.

private void contextMenuStrip_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
    if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
        e.Cancel = true;
}