Chapter 6. Inheritance and Reuse

Inheritance is at the heart of the Windows Forms architectureall 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 frameworkthird-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 carederiving from a class makes a strong statement about your own class: you are declaring that your class is everything that the base class is; inheritance defines an "is a" relationship. This makes sense when deriving from Control if you are building a visual component, then by definition, it is a control. But for less generic base classes such as TextBox , it is important to be clear whether your control really is a TextBox , or whether it simply uses one in its implementation.

We will therefore start by looking at the situations in which inheritance is an appropriate technique. Then, we will see how to inherit from classes based on Form and UserControl , both of which have special support from the IDE. Next we will look at inheriting directly from other controls. Finally, some of the pitfalls of inheritance will be examined.



. Net Windows Forms in a Nutshell
.NET Windows Forms in a Nutshell
ISBN: 0596003382
EAN: 2147483647
Year: 2002
Pages: 794

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net