|
Subclass Controls in .NET
Build an owner-draw listbox control using the new capabilities of .NET.
by Bill Storage
Technology Toolbox: VB.NET
You don't have to build custom controls from scratch. You can use inheritance to add specialized behavior to existing controls, as this month's Desktop Developer column describes (see Resources). But you don't have to stop there. For advanced control building, you'll want to master subclassing.
As far back as version 5, Visual Basic supported subclassing to some extent, but VB's lack of inheritance meant subclassing involved a lot of extra code. It took .NET's fully object-oriented architecture to really make it sing.
I'll introduce you to subclassing by showing you how to use it to build an owner-draw listbox control, with your starting point the standard .NET listbox. You'll find the origins of this example in a series of articles Matt Curland and I wrote a few years ago, showing you how to build a similar control in VB5 (see Resources). If you go back and see the VB5 approach, you'll appreciate how much easier subclassing has become. I'm not saying it has become trivial, but certainly easier.
I'll introduce you to several aspects of .NET control building in the course of this demonstration. Future articles will provide more detailed coverage of control design, with this article providing a technology overview.
Most control concepts remain the same with .NET, but now the term "subclassing" applies more literally—meaning both the interception and processing of Windows messages and the creation of derived types using inheritance. And most of the issues with debugging, instance management, and handling delicate (yet dangerous) pointers have gone away. This leaves me free to focus on what object orientation brings to control building, note some design patterns, and share the sheer delight (as far as programming goes) of using GDI+ (see Listing 1).
Back to top
|