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 forButton
...
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 delegateSystem.EventHandler
How do I fix this? Thanks for reading
You should introduce a cast inside the click
event handler
MouseEventArgs me = (MouseEventArgs) e;
You need MouseClick
instead of Click
event handler, reference.
switch (e.Button) { case MouseButtons.Left: // Left click break; case MouseButtons.Right: // Right click break; ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With