是否可以在选择/勾选某些项目后保持ContextMenuStrip菜单打开状态?
我计划使用简单的ContextMenuStrip设置一个筛选器(这样我可以在菜单或右键单击选项中使用相同的筛选器)。
该菜单列出了许多项目,我希望用户能够使用基本的检查功能进行选择。一旦选择完成,用户就可以单击“激活筛选器”选项,或单击菜单外部以激活或取消筛选器。
在选择/单击事件上,菜单通常会关闭。 是否可以在单击事件上保持菜单打开?
是否可以在选择/勾选某些项目后保持ContextMenuStrip菜单打开状态?
我计划使用简单的ContextMenuStrip设置一个筛选器(这样我可以在菜单或右键单击选项中使用相同的筛选器)。
该菜单列出了许多项目,我希望用户能够使用基本的检查功能进行选择。一旦选择完成,用户就可以单击“激活筛选器”选项,或单击菜单外部以激活或取消筛选器。
在选择/单击事件上,菜单通常会关闭。 是否可以在单击事件上保持菜单打开?
如果未来的程序员想知道如何做到这一点,这就是我发现的方法。这不会关闭上下文菜单,即使点击了任何项。创建上下文菜单关闭事件并设置一个if语句,以取消关闭事件,如果关闭原因是itemclicked。
private void contextMenuStrip_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
e.Cancel = true;
}
关闭事件
将e.Cancel设置为true以保持菜单打开状态
唯一的问题是该事件不会告诉您点击了什么,因此您需要自己跟踪。在要保持菜单打开的项目的单击事件中设置某种标志。然后在关闭事件中检查标志并适当地设置e.Cancel。
为了防止右键菜单在点击某个项目后关闭,请按照以下步骤操作。
在ContextMenuItems的mousedown事件中将标志设置为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
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;
}
}
}
ctxWildCards
只是我的ContextMenuStrip
,用于基于文件类型选择过滤器-用于搜索或FileDialogs) 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
我认为在ContextMenuStrip中没有这个属性。
我们在应用程序中使用的解决方法是,在ContextMenuStrip的clicked事件上进行一些处理,然后如果我们希望上下文菜单保持打开状态,我们只需再次调用ContextMenuStrip.Show。
如果ContextMenuStrip只有一级菜单,那么这将很好地工作。如果有子菜单和子子菜单,则必须重新选择单击之前打开的菜单,我不确定如何完成这项任务...
我发现这对我的目的很有用。
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
来读取标签或其他属性。我发现在没有闪烁的情况下实现这一点的最佳方法是使用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