DekGenius.com
[ Team LiB ] Previous Section Next Section

3.6 Extender Providers

Although the Control class provides a very rich set of features, inevitably it cannot be all things to all people. UI innovations continue to emerge, so even if the Control class were to represent the state of the art today, in time, it would inevitably end up looking short on features.

However, Windows Forms provides a very useful way of extending the abilities of the basic Control class. It is possible to place a component on a form that adds a feature to every single control on that form. Such a component is referred to as an extender provider. We will see how to write extender providers in Chapter 9, but no discussion of forms would be complete without looking at how to use them.

The Forms Designer supports extender providers. An extender provider can add new properties to all controls on a form. An example of this in the Windows Forms framework is the ToolTip class. As mentioned in Chapter 2, the Control class does not provide ToolTip support. But this doesn't matter—the framework has a ToolTip class that is able to augment any control with ToolTip support. If you drop the ToolTip component onto a form, it will appear in the component tray at the bottom of the designer. (All non-UI components appear here; the only kind of component that has any business appearing on the form at design time is a control, so everything else appears in the component tray. And the ToolTip isn't strictly a UI component; it is a component that modifies the behavior of other controls.) Once you have done this, if you look at the Properties tab for any of the controls on your form, you will see that each has acquired a ToolTip property in the Misc category. If you set some text for this property for a particular control, that text will appear as a ToolTip whenever the mouse hovers over that control at runtime.

Of course, the classes representing each control haven't really grown a new property—.NET doesn't allow class definitions to change at runtime. The extra property is an illusion presented by the Designer. If you set the ToolTip property on one of your controls in the designer, you will see that what really happens is that code like this is added to the C# InitializeComponent method:

this.toolTip1.SetToolTip(this.button1, "This is a button!");

or code like this is added to the VB InitializeComponent method:

Me.toolTip1.SetToolTip(Me.button1, "This is a button!")

Because we cannot really add a new property to somebody else's class, it is the responsibility of the extender provider to remember which controls have had their extender properties set to what. So the ToolTip class maintains a list of which controls have ToolTips and what the text is. It must also provide a method for setting the property. The name of that method is just the property name with Set in front of it. It takes a reference to the control whose property is being set and the property's value. (We will see in Chapter 9 how an extender provider tells the designer what extender properties it adds to the controls on a form.)

Whenever you use an extender provider, it will look like the previous code fragments. You will call a SetXxx method on the provider itself, passing in a reference to the control you would like to set the property on, and the value for the property. It is up to the provider to decide what to do with that value—for example, the ToolTip class attaches its own event handlers to the control and uses these to make the ToolTip appear when the mouse hovers over it.

    [ Team LiB ] Previous Section Next Section