Chapter 6. Inheritance and Reuse

Inheritance is at the heart of the Windows Forms architecture—all visual classes inherit from the Control class. Not only does this ensure that there is a single programming interface for common user interface functionality, it also guarantees consistent behavior because all controls inherit the same implementation. Control authors need only write code for the features unique to their components, and developers that use these controls will not be troubled by subtle differences in behavior. This is a welcome improvement over ActiveX controls, the previous visual component technology in Windows, where each control had to provide a complete implementation of the standard functionality, with not entirely consistent results.

Inheritance can offer further benefits when we write our own UI components. Not only can we take advantage of the Control class’s features, we can also derive from other controls such as Button or TextBox. We can extend the behavior of almost any control, not just those built into the framework—third-party controls can also be used as base classes. The Forms Designer provides support for two special cases of inheritance: forms that inherit from other forms, and controls that derive from other composite controls. It allows us to edit such inherited components visually.

As mentioned in the previous chapter, inheritance must be used with care—deriving from a class makes a strong statement about your own class: you are declaring that your ...

Get .NET Windows Forms in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.