不要关闭的ContextMenuStrip对某些项目的选择
-
22-08-2019 - |
题
是否有可能离开开放的ContextMenuStrip某些项目的选择/检查后?
我打算使用一个简单的ContextMenuStrip设置一个过滤器(这样我可以使用相同的过滤器或者在一个菜单或作为右击选项)。
在菜单中列出了一些项目,我希望用户能够做出选择使用基本功能检查的项目。一旦选择完成,用户可点击的激活过滤器选项,也可以点击菜单要么外面激活或取消该过滤器。
在选择/点击事件菜单通常关闭。 是否有可能保持在菜单上单击事件打开?
解决方案
要防止从文本菜单关闭被点击的项目时,执行以下操作。
在ContextMenuItems的mousedown事件设置的标志为假然后将其设置回真实在文本菜单的关闭事件。
示例:
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语句取消close事件。
private void contextMenuStrip_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
e.Cancel = true;
}
Closing事件
设置e.Cancel =真来退出菜单开放
唯一的问题是事件不会告诉你被点击的东西,所以你必须跟踪这个自己的。设置一些种类标志中要保持开放的菜单项的Click事件。然后在Closing事件检查该标志和适当地设定e.Cancel。
我不认为这是在对的ContextMenuStrip此属性。
我们在我们的应用程序中使用的解决方法是对的ContextMenuStrip的点击事件,我们做一些处理,然后如果我们想在上下文菜单中保持开放,我们只需再次呼吁ContextMenuStrip.Show。
这将很好地工作,如果仅存在一个电平到的ContextMenuStrip。如果有子菜单和子子菜单,那么你就必须重新选择是开放的前点击菜单,我不知道如何可以做到......
OnClosing,DO:e.Cancel = e.CloseReason = ToolStripDropDownCloseReason.CloseCalled; 然后当你决定关闭,调用Close()。
这是我的方法;它是无闪烁和 - 我想 - 一个更灵活一点。
如果您有一组ToolStripMenuItems你想为切换按钮(选项开/关)使用,试试这个:
(该ctxWildCards
只是我ContextMenuStrip
,用于基于文件类型来选择过滤器 - 用于搜索或FileDialogs)
这是在Visual Basic(明显;!),这样你就可以以编程方式或使用添加处理程序“把手......”条款
。 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.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;
}
}
}
我觉得这样做没有闪烁的最好方法是使用鼠标按下和鼠标离开事件在下拉菜单中的每个按钮。
示例:
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
来读取标签或其他一些属性。
我只想说清楚哪一个项目的上下文菜单打算以实现用户一个简单的项目。