Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Button.PerformClick do?

Tags:

c#

.net

winforms

I know this might be a trivial question, but I was just wondering whether there is any advantage of calling Button.PerformClick rather than invoking the click event of a button directly. The MSDN documentation simply says:

Generates a Click event for a button.

Does this mean it simply does the same thing as calling the click event of the button or is there some other special advantage?

like image 629
rtuner Avatar asked Dec 15 '22 07:12

rtuner


1 Answers

An external caller who knows nothing of the subscribed events cannot simply call the click handler - and events do not allow you to obtain information about subscribers. So this method allows separation of concerns, so that external callers can "play nice".

Additionally:

  • it ensures that any polymorphism on the virtual method is applied
  • it applies any rules - for example: is the button disabled

If you do know about the event-handler, and you aren't using polymorphism, and you don't care whether it is disabled, and you don't need to worry about event-handlers you don't already know about - then by all means : just call the event-handler method.

like image 192
Marc Gravell Avatar answered Dec 18 '22 10:12

Marc Gravell