25

I am creating a subclass of Button and would like to add custom functionality to some of its events such as OnClick. Which is the more desirable way to do it? Do I override OnClick:

protected override void OnClick(EventArgs e)
{
    base.OnClick(e);
    doStuff();
}

or should I instead link up the OnClick event to an event handler defined in my Button subclass through the designer?

class ButtonSubclass
{
    public ButtonSubclass() : base()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        this.Click += new System.EventHandler(this.ButtonSubclass_Click);
    }
}

Edit: I added minor visual changes (that may pass as rudimentary skinning) but most of the changes are in event handlers that I don't want to re-implement (copy-paste) on every form that reuses it.

4 Answers 4

28

If you're genuinely specializing the button, it makes sense to override OnClick. If you're only actually changing what happens when a button is clicked, I wouldn't subclass Button in the first place - I'd just add event handlers.

EDIT: Just to give a bit more of an idea - if you want to add similar event handlers for multiple buttons, it's easy enough to write a utility method to do that, and call it from multiple places. It doesn't require actual subclassing. That's not to say subclass is definitely wrong in your case, of course - just giving you extra options :)

4
  • 1
    Could you define "specializing" in this context? Dec 19, 2012 at 19:00
  • @Ken: Making it behave in some genuinely different way, e.g. executing all the handlers in reverse order. Not just "a button which always has a particular handler".
    – Jon Skeet
    Dec 19, 2012 at 21:57
  • I like the answer, however - when one makes a more specialized form in .net one is encouraged to catch the onclick event, onload event.
    – timothy
    Apr 19, 2016 at 10:47
  • Its actually all about reuse. Override the method or implement the event, on both occasions you are specialising the behaviour. If you would like to change the behaviour of the button and reuse the entire button expecting it to behave the new way, then override, otherwise catch the event and implement leaving the button unchanged for next time.
    – timothy
    Sep 21, 2017 at 11:08
7

Always override OnClick when inheriting. It gives you better performance.

6
  • 4
    Performance should be deemed of secondary importance to readability in most cases, until the impact has been proven to be significant. I don't believe the difference in performance is significant here (it's quite small, and we're talking button clicks here, i.e. relatively infrequent events).
    – Jon Skeet
    Mar 18, 2009 at 10:27
  • I'm not saying that overriding OnClick is wrong - just that performance isn't the reason to do it.
    – Jon Skeet
    Mar 18, 2009 at 10:28
  • Maybe the performance gain is insignificant but if you are already subclassing for some other reason that to just override OnClick then I think it is both easier to read, faster to implement, and then you gain the performance gain "for free". Mar 18, 2009 at 10:31
  • So the reasons to give are "easier to read, faster to implement" rather than "it gives you better performance". That was my point. The performance gain is incidental.
    – Jon Skeet
    Mar 18, 2009 at 10:41
  • Performance for 1 extra event handler is significant if the UI element is instantiated multiple times. E.g. in an ItemsControl.
    – l33t
    Sep 8, 2016 at 8:48
0

Actually, there is one advantage of attaching an event handler over overriding a method. Your event handler is private, while your overridden method has to be protected with a predefined name. If you use obfuscating/encrypting tools to protect your application, your code is more exposed when it's protected override than when private.

1
  • Please explain. My code is compiled. Why would a private have more security of a protected member. Did you mean code of a private member will not be visible and a protected code is visible? Oct 20, 2022 at 6:59
-2

For a button click, I'd keep it as a method for reading. But I guess it is a personal preference thing. I'd be interested to see what the concensus is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.