views:

1171

answers:

5

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?

+1  A: 

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...

Meta-Knight
Thanks! I checked and this works perfect for one level menus. I will leave the question open for a while longer to see if there is another option.
barry
+1  A: 

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.

This seems to work more elegant than the show solution. I set the e.Cancel to true in the Closing event of the ContextMenu based on a flag as suggested and set the flag on the individual clicked items. However since the Cancel event is only set during the closing event it lags one step behind. Is there a way to set the Cancel flag in for the closing event in the item click events?
barry
+2  A: 

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:

Private blnClose As Boolean = True

Private Sub MoveUpToolStripMenuItem_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MoveUpToolStripMenuItem.MouseDown

 blnClose = False

End Sub

Private Sub ContextMenuStrip1_Closing(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripDropDownClosingEventArgs) Handles ContextMenuStrip1.Closing

 e.Cancel = Not blnClose
 blnClose = True

End Sub

A: 

How can this be done in Compact Framework?

eam
A: 

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