如何获得右击鼠标事件?更改的EventArgs到MouseEventArgs导致Form1Designer错误? [英] How to get a right click mouse event? Changing EventArgs to MouseEventArgs causes an error in Form1Designer?

查看:1228
本文介绍了如何获得右击鼠标事件?更改的EventArgs到MouseEventArgs导致Form1Designer错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检测左击事件的Visual Studio的表格上双击了一个方法。

I have a method to detect the left click event that visual studio made by double clicking on the form.

 private void pictureBox1_Click(object sender, EventArgs e)
 {
 MessageBox.Show("Left click");
 }



我想有通过右键单击在同一个右键单击事件。对象

I want to have a right-click event by right-clicking on the same object.

我看网上,你可以使用此开关:

I read online that you can use this switch:

 private void pictureBox1_Click(object sender, EventArgs e)
 {
 if (e.Button == System.Windows.Forms.MouseButtons.Right){MessageBox.Show("Right click");}
 if (e.Button == System.Windows.Forms.MouseButtons.Left){MessageBox.Show("Left click");}
 }

麻烦的是,当我做到这一点e.Button已经有一条红线和错误:

The trouble is that when I do this e.Button has has a red line and error:

 "'System.EventArgs' does not contain a definition for 'Button'... "

于是我改变了EventArgs五到MouseEventArgs E

So I fix this by changing the "EventArgs e" to "MouseEventArgs e"

解决这个问题但再有就是Form1Designer凡在活动一个新的错误行是:

But then there is a new error in Form1Designer where the event line is:

 this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);



错误说:

The error says:

"No overload for 'pictureBox1_Click' matches delegate 'System.EventHandler' "

我该如何解决这个问题?
感谢您阅读

How do I fix this? Thanks for reading

推荐答案

您应该介绍一下里面的点击事件处理程序

You should introduce a cast inside the click event handler

MouseEventArgs me = (MouseEventArgs) e;

这篇关于如何获得右击鼠标事件?更改的EventArgs到MouseEventArgs导致Form1Designer错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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