views:

31

answers:

4

I have a Button and I need to fire it's Click event externally from actually clicking it. However, the code

myButton.Click(this, EventArgs.Empty);

gives me the error

The event 'System.Windows.Forms.Control.Click' can only appear on the left hand side of += or -=

How can I fix it?

A: 

Why not just fire the event directly?

If you're event is called myButton_Click you can simply call it from wherever in the page

myButton_Click(myButton, EventArgs.Empty);

Marko
I could do that, but i'm trying to do it this other way ;)
RCIX
A: 

Click is an event, not a method.

You can invoke the method witch one has added to the event.

Tim Li
+2  A: 
Button button = new Button();
button.PerformClick();
devnull
+2  A: 

The Button type has a PerformClick method that does exactly what you want.

Dan Tao
What confused me is that in my own events i call the event like a method, and i was wondering why i couldn't do so here.
RCIX