开发者

Firing a Button's Click event from outside Button

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

myButton.Click(this, Ev开发者_开发问答entArgs.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?


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


Button button = new Button();
button.PerformClick();


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


Click is an event, not a method.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜