.
GDI+ FAQ
Skip Navigation LinksWelcome : Windows Forms Tips and Tricks : To override or handle?

To Override Or To Handle?

Windows Forms controls expose many methods and events that can be used to extend the functionality of an object but what is the best method, overriding a virtual method or adding a handler to an event?

Obviously, if you use a stock component such as a button or label and you want to add functionality in your application that responds to an event such as a Paint or Click then the choice is clear, you simply add a handler to the event provided and create the code that performs the work in some method of your class. If, on the other hand, you create a class of your own derived from some other control, you have a potential choice, to override one of the protected methods or to add a handler to the class events. Lets look at the possibilities.

The following listing shows a simple derived UserControl which does something whenever the size of the control changes. In the first instance, the SizeChanged event is handled and in the second instance the OnSizeChanged virtual method is overidden. These user controls were built with Visual Studio and the handlers added to the control using the handler list in the editor.

Show the handled control in C#

Show the handled control in VB

Show the overridden control in C#

Show the overridden control in VB

Both incarnations of this user control simply resize their contained panels to cover 50% of the control height whenever they are resized. They both do exactly what they should and so in this case, either method is as good. However, a problem arises when the handled versions of the controls need to have their own behaviour modified.

The code generated by Visual Studio has the InitializeComponent method and the SizeChanged event handler marked with private access. You cannot override InitializeComponent to remove the addition of the event handler, you can't specify the event-handler method to remove it from the list of events and you can't override the event handler itself so, if you ever wanted to modify the control, for example to change the way the panel layout was arranged, the original behaviour would still remain in place. Adding another event handler is of course possible but remember that events can have more than one handler tied to them and they are all called in order. For this simple demonstration, the overhead of laying out the panels twice wouldn't matter but if the process were complex or time-consuming because it relied on some input-output function, the overhead could mean a real performance hit for your control.

Generally, if you are deriving from a control, always override. A control should never handle it's own events because to do so can seriously effect the rules of object-orientation.

Return to Windows Forms Tips and Tricks.

Sponsored By
DaraizeTechnologies.com

&
Proteus Groupe

Bob Powell

Create your badge

Copyright © Bob Powell 2000-2012.  All rights reserved.