检测右键单击工具条按钮 [英] Detect right click on toolstrip button
问题描述
我有一个带有上下文菜单的工具栏和一个带有click事件的工具栏按钮. 最初,我尝试将上下文菜单分配给按钮本身,但是在其属性中找不到上下文菜单. 因此,我将上下文菜单分配给了工具栏. 现在,每当我右键单击要显示上下文菜单的按钮时,都会触发按钮单击事件. 我想检查单击了哪个鼠标按钮,所以我很累于将事件args强制转换为mouseeventargs:
I have a toolstrip with a context menu and a toolstrip button in it with a click event. Initially I tried to assign the context menu to the button itself, but couldn't find a context menu in its property. So I assigned the context menu to the toolstrip. Now whenever I right click the button for the context menu to appear, the button click event is triggered. I want to check which mouse button is clicked, so I tired to cast event args to mouseeventargs:
if (((MouseEventArgs)e).Button != MouseButtons.Left) return;
但是我有一个例外,我无法执行此强制转换. 我可以将上下文菜单分配给按钮还是可以检测单击了哪个鼠标按钮? 谢谢
but I got an exception that I can't do this cast. Can I either assign context menu to the button or detect which mouse button is clicked? Thanks
推荐答案
您可以像这样尝试ToolStripButton
的MouseDown
事件:
You can try the MouseDown
event of the ToolStripButton
like this:
private void toolStripButton1_MouseDown(object sender, MouseEventArgs e){
if(e.Button == MouseButtons.Right){
//...
}
}
这篇关于检测右键单击工具条按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!