Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

c# - How to get a right click mouse event? Changing EventArgs to MouseEventArgs causes an error in Form1Designer?

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");}
}

The trouble is that when I do e.Button it has has yields an error error:

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should introduce a cast inside the click event handler

MouseEventArgs me = (MouseEventArgs) e;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...