检测按钮是右击还是左击 [英] Detect whether button was right or left clicked

查看:16
本文介绍了检测按钮是右击还是左击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题就像我认为的那样简单但它变得有点复杂,我试图实现的是:我的 WinForm 中有几个按钮几乎可以做同样的事情,所以我创建了一个事件来处理所有这些,现在我希望根据他们单击的鼠标按钮为他们提供一些其他功能,例如,如果左键单击按钮执行它一直在做的事情,但如果它的右键单击执行其他方式,这可以实现吗?,任何帮助将不胜感激.

my problem was as i believed simple but it turned to be a little more complicated, what i was trying to achieve was this: I have several buttons in my WinForm that do almost the same thing, so I've created a single event to handle all of them, now what i wished was to give them some other functionality based on which mouse button they clicked, for instance if the button is left clicked performs as it has been doing but if its right clicked performs in some other way, can this be achieved?, any help would be greatly appreciated.

我正在寻找这样的声明,也许...

I'm looking for some statement like this maybe...

 private void buttons_Click(object sender, EventArgs e)
         {
 if(e.buttons==mousebuttons.right)
 //do something 
 else
 //do other thing
 }

正如您所知,这是无法实现的,因为 e 上没有 mouse button events,因为它被称为 >按钮点击事件.

the thing as you know is that this can't be achieved since e has no mouse button events on it, cause i'ts being called as a button click event.

推荐答案

它似乎不适用于单击事件.但是 MouseDown 事件会给你一个 MouseEventArgs ,它有一个 button 属性.那么也许您可以修改您的代码以使用 MouseDown 而不是单击?

It doesn't seem to work with the click event. But the MouseDown event will give you a MouseEventArgs that has a button property. So perhaps you can modify your code to use MouseDown istead of click?

var btn = new Button();
btn.Text = "BUTTON";
btn.MouseDown += ((o, e) => {
    MessageBox.Show(e.Button.ToString());
});

这篇关于检测按钮是右击还是左击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆