不要在选择某些项目时关闭ContextMenuStrip

在选择/检查某些项目后,是否可以打开ContextMenuStrip?

我计划使用一个简单的ContextMenuStrip来设置filter(这样我可以在菜单中使用相同的filter或作为右键单击选项)。

菜单列出了许多项目,我希望用户能够使用基本检查function选择项目。 完成选择后,用户可以单击“激活”filter选项,也可以单击菜单外部以激活或取消filter。

在选择/单击事件上,菜单通常会关闭。 是否可以在点击事件中保持菜单打开?

要防止在单击项目时关闭上下文菜单,请执行以下操作。

在ContextMenuItems的mousedown事件上将flag设置为false,然后在contextmenu的closing事件处将其设置回true。

例:

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 

如果未来的程序员想知道如何做到这一点,这就是我想到的。 如果单击任何项​​目,则不会关闭上下文菜单。 如果关闭原因是itemclicked,则创建上下文菜单条关闭事件并设置if语句以取消关闭事件。

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

闭幕活动

设置e.Cancel = true使菜单保持打开状态

唯一的问题是事件没有告诉你点击了什么,所以你必须自己跟踪这个。 在要保持菜单打开的项目的Click事件中设置某种标志。 然后在Closing事件中检查标志并适当地设置e.Cancel。

我不认为ContextMenuStrip中有这个属性。

我们在应用程序中使用的变通方法是在ContextMenuStrip的单击事件上,我们进行一些处理,然后如果我们希望上下文菜单保持打开,我们只需再次调用ContextMenuStrip.Show。

如果ContextMenuStrip只有一个级别,这将很有效。 如果有子菜单和子子菜单,那么你必须重新选择在点击之前打开的菜单,我不知道如何做到这一点……

这是我的方法; 它没有闪烁 – 我认为 – 更灵活一点。

如果你有一组ToolStripMenuItems你想用作切换按钮(选项开/关),试试这个:

ctxWildCards只是我的ContextMenuStrip ,用于根据文件类型选择filter – 用于搜索或FileDialogs)

这是在Visual Basic中(显然!;),因此您可以以编程方式或使用“Handles …”子句添加Handlers。

  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, ie 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 

我发现奇怪的是ContextMenuStrip.Closing事件 ToolStripMenuItem.Click事件之前触发。 解决方案是使用ContextMenuStrip.ItemClicked事件,其中有e.ClickedItem ,然后检查它是否是其中一个项目,当单击时,不会关闭ContextMenuStrip ,并设置适当的标志。 然后在ContextMenuStrip.Closing中设置e.Cancel = true; 如果标志也设置。 不要忘记重置旗帜。

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

OnClosing,do:e.Cancel = e.CloseReason!= ToolStripDropDownCloseReason.CloseCalled; 然后当你决定关闭时,调用Close()。

我发现没有闪烁的最佳方法是在DropDown菜单中为每个按钮使用MouseDown和MouseLeave事件。

例:

 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 

我发现这对我的目的很有用。

 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 

您可以使用ItemClicked来读取标记或其他属性。

我只想要一个简单的项目,向用户明确上下文菜单将要生效的项目。