加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何获取右键单击鼠标事件?将EventArgs更改为MouseEventA

发布时间:2020-12-15 17:49:38 所属栏目:百科 来源:网络整理
导读:我有一种通过双击表单来检测可视化工作室制作的左点击事件的方法. private void pictureBox1_Click(object sender,EventArgs e) { MessageBox.Show("Left click"); } 我想通过右键单击相同的对象来右键单击事件. 我在线阅读可以使用此开关: private void pi
我有一种通过双击表单来检测可视化工作室制作的左点击事件的方法.
private void pictureBox1_Click(object sender,EventArgs e)
 {
 MessageBox.Show("Left click");
 }

我想通过右键单击相同的对象来右键单击事件.

我在线阅读可以使用此开关:

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有一个红线和错误:

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

所以我通过将“EventArgs e”更改为“MouseEventArgs e”来解决这个问题

但是,在Form1Designer中有一个新的错误,事件行是:

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

错误说:

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

我该如何解决?
谢谢阅读

解决方法

您应该在点击事件处理程序中引入一个转换
MouseEventArgs me = (MouseEventArgs) e;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读