Derik Whittaker



Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at
Subclassing for Success

As someone how has been designing and developing WinForms based applications for the past 7+ years, I have learned that subclassing UI controls is a must.  You may be thinking, why would I want to subclass a text box?  Or a combo box?  The answer is simple, the better future proof your app. 

How many times have you been into the development cycle of your application and you found out that you needed/wanted to extend the text box that is being used.  By extend I am not necessary talking about adding functionality to it, but do something like having the control highlight the entire contents of the control when the it receives focus.

If you don't have your controls subclassed, extending your control will be a pain in your ass.  Sure you could find EVERY use of the control and add the needed code.  Or you could find and replace your control later with the subclassed control, but both of these suck.

When I start a new WinForms (same concept should work for Browser based as well) application, I will immediately add a Controls namespace and simply add my subclassed controls.  In many cases I never extend the control, but knowing that I can do so allows me to make better decisions later on. 

One example of where this has helped me in the past was on a project where about 6 months into development, the business owners decided that would like the UI controls (text box, check box, combo box, etc) to change background color when the control receives focus and then change back when it loses focus.  Because I had everything subclassed i was able to implement this request in a very short time frame and did not even have to open up a single form.  Had I not subclasses the controls I would have either pushed back heavily or it would have taken me much longer to implement this change.

Another good use is for things like data grids.  Whether you are using the out of the box data grid, or some third party grid like Infragistics or Janus, subclassing can save you a ton.  How many times have you developed an application where you wanted a standard look and feel for your grids.  Say every other line is gray, and there are no selection column.  If the control is not subclassed you are asking each developer to remember to make the look and feel customizations.  But, if it is subclassed, simply add the control to the form and you are done.

Till next time,

Posted 11-02-2007 7:36 AM by Derik Whittaker



Mike Breen wrote re: Subclassing for Success
on 11-02-2007 10:31 AM

Why wouldn't you choose composition over inheritance?

Keith Nicholas wrote re: Subclassing for Success
on 11-02-2007 10:15 PM

Mike:  Composition is good at the layer objects live in.... in this case subclassing them into your own "Controls" layer is good.  From there, within your "Controls" layer you can compose them together into your own "new" controls if you like.    You have to design for composibility..... which winform controls are only semi good at.     So subclassing to a new layer and then making it composable is good.    WPF on the other hand is super composable.  

I think the technique is good,  however I do dispute the amount of pain it would be to swap from, say, TextBox to "MyTextBox",  its reasonablly trivial to do.   Though I still think its a good idea to do from the start.

Mike Breen wrote re: Subclassing for Success
on 11-03-2007 9:51 AM

@Keith - thanks for the explanation.

Gavin Colwell wrote re: Subclassing for Success
on 11-04-2007 9:53 PM

Hi... nice blog... you have some interesting articles....

I find subclassing webcontrols is essential when building apps.  Usually, there are a certain number of controls that require some Javascript (e.g. - masked text boxes, confirm buttons, etc) and I find it very useful to encapsulate this functionality in our own custom webcontrols.  It makes maintenance and enhancements a doddle.  It also creates cleaner code in my opinion.

In addition, by putting these in a separate assembly outside the web project, they can be re-used on future projects.

routes assumptions 1980 place reconstructions wrote re: Subclassing for Success
on 05-16-2009 2:35 AM

ruddiman increased processes present retrieved

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)