Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

I'm wondering if anyone's seen any reason why overriding an event on a form is better/worse than adding an event handler. Not counting the overhead of calling the base class's event and not counting the slight overhead of adding an event handler...

 

For example, to use the Form_Load event, you can use:

// In InitializeComponent
this.Load += new System.EventHandler(this.Form1_Load);

// Farther down, the actual function:
private void Form1_Load(object sender, System.EventArgs e)
{
   // Do something here
}

 

or you could use:

protected override void OnLoad(System.EventArgs e)
{
   // Do something here

   // Finally, call base class
   base.OnLoad(e);
}

 

At my company we've always used the event handler method, mostly because it's easier but somewhat out of ignorance (we had a lot of developers coding who didn't know you could use the override OnLoad).

 

With hiring some new people I'm in the process of reviewing our coding standards document and wanted to get some outside opinions. Maybe Microsoft has recommended a certain method? Maybe it's like variable naming - just pick a standard and use it consistently? Or maybe there's something we're missing...?

 

 

Thanks all!

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • *Experts*
Posted
I use event handlers, because you have more control; you can remove/add the event handlers at runtime, and change their names if you want from OnWhatever if you want.
  • *Gurus*
Posted
When you are writing custom controls the recommended method is to override (calling the base of course); however, I don't think there is a recommended method for just writing code for a form. Personally I use the events.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

I would always handle a form event by way of overriding the event method (and calling the base class's method). You are the form, so it's only right that your event method gets called by the framework directly. If you do this by way of attaching an event handler, it's as though you're an outsider who just happens to be interested in one of the form's events. (This would be like, for example, when you attach an event handler to a Button member's Click event. You are not the button itself; you are just interested in when something happens to the button.)

 

If you look at the documentation for any form event method, you'll see the following text:

 

The OnBlah method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...