tags:

views:

3214

answers:

4

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.

+2  A: 

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

Jakob Christensen
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
I'm not saying that overriding OnClick is wrong - just that performance isn't the reason to do it.
Jon Skeet
performance for 1 extra eventhandler is a bit rediculous
PoweRoy
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".
Jakob Christensen
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
You are right. Thanks :-)
Jakob Christensen
+11  A: 

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

Jon Skeet
A: 

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.

Francis Gilbert
+2  A: 

From an object oriented perspective it's better to minimize the use of events (most Java folks survive without delegates and events :) )

maybe a bit offtopic, nut in ASP.NET, I always override the methods (like OnLoad) rather than using the event handlers (Page_Load) to prevent "AutoEventWireUp" confusion.

I normally like to disable "AutoEventWireUp" completely because it works very non-transparent but in the case where someone enables it again you might end up with duplicate handlers, causing them to be fired twice.

I guess for Windows Forms it's also more transparent to override methods, as it doesn't involve the risk of accidentally adding multiple event handlers, and I dislike the camelCased underscore (_) syntax of automatically created handlers.

Zidad
Having done work in Java, I too feel uncomfortable with the automatically generated handlers and difficulties with the way C# (at least the forms designer) manages multiple event handlers.
Jeremy