9.7 Creating Extender Provider Controls

 <  Day Day Up  >  

You want to create a control that works with several controls on a Windows Form similar to the ToolTip control.


Technique

Create a new class derived from System.ComponentModel.Component and System.ComponentModel.IExtenderProvider . Because IExtenderProvider is an interface, you must implement its sole method named CanExtend . This method is called whenever a control on the form is selected in the forms designer. Within the method definition, check the type of the object passed as the parameter. If it is a control type that your extender provider supports, return a true value from the method:

 
 bool IExtenderProvider.CanExtend(object target) {     if (target is Control && !(target is BalloonTip))     {         return true;     }     else     {         return false;     } } 

An extender provider inserts additional properties into each control that it extends. For each property that you want to add to a control, apply the ProvideProperty attribute to the class declaration. The first parameter is a string specifying the name of the property, and the second parameter is the type of the objects that receive the property, which in most cases is System.Windows.Forms.Control :

 
 [     ProvideProperty("BalloonText",typeof(Control)) ] public class BalloonTip : Component, System.ComponentModel.IExtenderProvider { ... } 

The implementation for each extended property uses methods with a defined syntactical name rather than a property-based syntax. You construct the getter method for an extended property by using the word Get followed by the name of the property. For the BalloonText property shown earlier, the getter method would be named GetBalloonText . Likewise, the setter method uses the Set word, which translates into SetBalloonText :

 
 private Hashtable balloonTexts; [DefaultValue("")] public string GetBalloonText(Control control) {     string balloonText = (string)balloonTexts[control];     if (balloonText == null)     {         balloonText = string.Empty;     }     return balloonText; } public void SetBalloonText(Control control, string balloonText) {     if (balloonText == null)     {         balloonText = string.Empty;     }     if (balloonText.Length == 0)     {         balloonTexts.Remove(control);         control.MouseHover -= new EventHandler(OnControlMouseHover);         control.MouseMove -= new MouseEventHandler(OnControlMouseMove);     }     else     {         balloonTexts[control] = balloonText;         control.MouseHover += new EventHandler(OnControlMouseHover);         control.MouseMove += new MouseEventHandler(OnControlMouseMove);     } } 

Comments

An extender provider control generally provides additional information about another control on a Windows Form. For example, the ToolTip control displays help information in a small window whenever the mouse cursor hovers over a control for a specified amount of time. You insert the additional information for a control by using properties. Once you place the extender provider using the forms designer, any controls on the Windows Form that are supported by the extender provider have additional properties that are implemented by the extender provider control. The name of the property is the name used within the extender provider, specified using the ProvideProperty attribute followed by the word on and the variable name of the extender provider object. For instance, if you create a BalloonTip object within a Windows Form named balloonTip1 , then each control on the Windows Form has an additional property named BalloonText on ballonTip1 .

We mentioned that an extender provider works with several controls at once on a single Windows Form. This implies that you'll need a collection object to save the values that your extender provider adds to each control it supports. In most instances, you'll find that the best collection type for this need is a Hashtable , where each key within the collection represents a control and each associated value is the corresponding property value. If your extender provider control utilizes only a single property, then the Hashtable collection can store values using the data type of that property. However, if your extender provider supports several properties per control it supports, then use a second Hashtable object, where the key for each element represents the property name and the value is that property's value.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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