Do not close ContextMenuStrip on selection of certain items

我是研究僧i 提交于 2019-12-08 15:57:42

问题


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:


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



回答2:


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;
}



回答3:


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.




回答4:


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




回答5:


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




回答6:


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 my ContextMenuStrip, 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.

  Private Sub OnOffToolStripMenuItem_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) 

    Dim t = TryCast(sender, ToolStripMenuItem)
    If Not t Is Nothing Then
        'Since you may have more On/off-Items, check to see if the Owner is the ContextMenuStrip 
        If t.Owner Is ctxWildCards Then
           ' The ContextMenuStrip will stay open on Right-click, i.e. the user can check and close by clicking 'normally'
            ctxWildCards.AutoClose = (e.Button = Windows.Forms.MouseButtons.Left)
        End If
        'Just me using a custom image for checked items.
        t.Checked = Not t.Checked
        t.Image = If(t.Checked, rdoImage, Nothing)
    End If
  End Sub

 ' On leaving ToolStripMenuItems of the ContextMenuStrip, allow it to AutoClose
  Private Sub OnOffToolStripMenuItem_MouseLeave(sender As System.Object, e As System.EventArgs)
  ctxWildCards.AutoClose = True
End Sub



回答7:


What i found strange is that ContextMenuStrip.Closing event fires before the ToolStripMenuItem.Click event. The solution was to use ContextMenuStrip.ItemClicked event where you have e.ClickedItem, and then check if it's one of the items that, when clicked, won't close the ContextMenuStrip, and set the appropriate flag. Then in ContextMenuStrip.Closing you can set e.Cancel = true; if the flag is also set. Don't forget to reset the flag though.

bool isRunAtStartupClicked;
private void ContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{   
    if (e.ClickedItem == trayIcon.ContextMenuStrip.Items["miRunAtStartup"])
    {   
        isRunAtStartupClicked = true;
    }
}

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



回答8:


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:

Private Sub ToolStripMenuItem2_Mousedown(sender As Object, e As EventArgs) Handles ToolStripMenuItem2.MouseDown
        ΥπηρεσίεςToolStripMenuItem.DropDown.AutoClose = False
End Sub

Private Sub ToolStripMenuItem2_MouseLeave(sender As Object, e As EventArgs) Handles ToolStripMenuItem2.MouseLeave
        ΥπηρεσίεςToolStripMenuItem.DropDown.AutoClose = True
End Sub



回答9:


I found this useful for my purposes.

Private Sub CM_Closing(sender As Object, e As ToolStripDropDownClosingEventArgs) Handles CM.Closing
    If e.CloseReason = ToolStripDropDownCloseReason.ItemClicked Then
        Dim ItemClicked As String = CM.GetItemAt(New Point(Cursor.Position.X - CM.Left, Cursor.Position.Y - CM.Top)).Name
        If ItemClicked = "CMHeader" Then
            e.Cancel = True
        End If
    End If
End Sub

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.



来源:https://stackoverflow.com/questions/866689/do-not-close-contextmenustrip-on-selection-of-certain-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!