Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not close ContextMenuStrip on selection of certain items

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?

like image 891
barry Avatar asked May 15 '09 01:05

barry


2 Answers

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;
}
like image 80
Delirious Avatar answered Nov 10 '22 00:11

Delirious


the Closing event

set e.Cancel = true to leave the menu open

only problem is the event doesn't tell you what was clicked, so you have to keep track of this yourself. set some kind of flag in the Click event of the items you want to keep the menu open. then in the Closing event check the flag and set e.Cancel appropriately.

like image 4
user89894 Avatar answered Nov 09 '22 23:11

user89894