显式阻止 ContextMenuStrip 在 C# 中加载
我需要在未设置特定标志时隐藏上下文菜单条。 由于我认为我们无法显式控制上下文菜单条的显示/隐藏,因此我决定在与上下文菜单条关联的控件上捕获鼠标右键单击。 它是一个 UserControl,所以我尝试处理它的 MouseClick 事件,在其中检查是否设置了标志以及按钮是否是右键。 然而令我惊讶的是,该事件不会在鼠标右键单击时触发,而是仅在左键单击时触发。
我有什么问题或者有什么解决方法吗?
检测到右键单击,问题标题和描述已更改
经过更多研究,当我处理控件上的 Mouse_Down 事件时,我得到了右键单击。 然而,我仍然一无所知,如何显式阻止 ContextMenuStrip 加载。 另一个问题是,为什么 MouseClick 没有检测到右键单击?
当前的解决方法
注册事件处理程序
userControl1.Control1.MouseDown += new MouseEventHandler(Control1_MouseDown);
void Control1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && flag == false)
{
userControl1.Control1.ContextMenuStrip = null;
}
else
{
userControl1.Control1.ContextMenuStrip = contextMenuStrip1;
}
}
这是我正在做的当前解决方法。 但是我如何在 ContextMenuStrip 的打开事件中更改它
I have a requirement to hide the contextmenustrip when a particular flag is not set. As i don't think we can explicitly control the show/hide of the context menu strip, i decided to trap the right mouse button click on the control with which the contextmenustrip is associated. It is a UserControl, so i tried handling it's MouseClick event inside which i check if the flag is set and if the button is a right button. However to my amazement, the event doesn't get fired upon Mouse Right Click, but fires only for Left Click.
Is there any thing wrong with me or is there any workaround?
RIGHT CLICK IS GETTING DETECTED, Question TITLE and Description Changed
After Doing some more research, i got the rightclick to fire, when i Handled the Mouse_Down Event on the Control. However Am still clueless, as how to explicitly prevent the ContextMenuStrip From Loading. Another question being, why MouseClick didn't detect Right Button Click?
Current WorkAround
Registering the event handler
userControl1.Control1.MouseDown += new MouseEventHandler(Control1_MouseDown);
void Control1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && flag == false)
{
userControl1.Control1.ContextMenuStrip = null;
}
else
{
userControl1.Control1.ContextMenuStrip = contextMenuStrip1;
}
}
this is the current workaround i'm doing.
But how can i change it in the Opening Event of the ContextMenuStrip
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当使用键盘上的上下文菜单键(或它的名称)调用上下文菜单时,您的解决方案无论如何都会失败。 您可以使用
Opening
事件取消打开上下文菜单。Your solution will fail anyway when the context menu is invoked with the context menu key (or what it's called) on the keyboard. You can use the
Opening
event to cancel the opening of a context menu.有一个解决方法。
假设菜单项 A 设置控制控件 B 上的上下文菜单的标志。
在 A 的单击事件中,设置 b.ContextMenu = Nothing 将其关闭,并将 b.ContextMenu 设置回上下文菜单控件以将其切换回来。
There is a work around.
Lets say Menu Item A sets the flag that controls the context menu on control B.
In the click event for A, you set b.ContextMenu = nothing to switch it off, and set b.ContextMenu back to the context menu control to switch it back on.
在 WinForms 中还有
Click
事件 - 单击鼠标右键会触发该事件。如果您使用的是 WPF,则应该有
MouseRightButtonDown
和MouseRightButtonUp
事件。查看此 MSDN 页面 列出了哪些控件引发哪些单击事件。 值得注意的是,Button、CheckBox、RichTextBox 和 RadioButton(以及其他一些)不会在右键单击时引发事件。
In WinForms there's also the
Click
event - which does get fired for a right mouse click.If you are using WPF you should have
MouseRightButtonDown
andMouseRightButtonUp
events.Check out the table on this MSDN page which lists which controls raise which click events. Significantly, Button, CheckBox, RichTextBox and RadioButton (and a few others) don't raise and event on right click.