Do not close ContextMenuStrip on selection of cert

2019-06-15 08:18发布

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?

9条回答
我欲成王,谁敢阻挡
2楼-- · 2019-06-15 08:43

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.

查看更多
我想做一个坏孩纸
3楼-- · 2019-06-15 08:46

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;
}
查看更多
Explosion°爆炸
4楼-- · 2019-06-15 08:47

OnClosing, do: e.Cancel = e.CloseReason != ToolStripDropDownCloseReason.CloseCalled; and then when you decide to close, call Close().

查看更多
登录 后发表回答