다음을 통해 공유


Extender Provider Objects

An extender provider is a component that provides properties to other components. For example, when a ToolTip component is added to a form, it provides a property called ToolTip to each control on that form. The ToolTip property then appears in the Properties window for each control and allows the developer to set a value for this property at design time.

The property provided by the extender provider actually resides in the extender provider object itself and therefore is not a true property of the component it modifies. At design time, the property will appear in the Properties window for the component that is being modified. At run time, however, the property cannot be accessed through the component itself. In the following code example, a form has been created with a button called MyButton and a ToolTip control called MyToolTip, which provides a ToolTip property.

' This is an example of code that is NOT CORRECT!
Dim myString as String
myString = MyButton.ToolTip
// This is an example of code that is NOT CORRECT!
string myString;
myString = MyButton.ToolTip;

This syntax will generate a compile error because the compiler does not recognize ToolTip as a property of MyButton, because the property is actually provided by MyToolTip. The following example shows how to correctly access that property:

Dim myString as String
myString = MyToolTip.GetToolTip(MyButton)
string myString;
myString = MyToolTip.GetToolTip(MyButton);

The extender provider is a class, and as such can have its own properties and methods. In order to designate a property as the property to be provided to other components, you apply the ProvidePropertyAttribute attribute at the class level. This attribute specifies the name of the property to provide and the type of object it can provide that property to. By convention, the property provided is not implemented as a property, but rather as a pair of methods. These methods must have "Get" and "Set" added to the beginning of the name of the property to be provided. The following example shows how.

Imports System.ComponentModel
<ProvideProperty("MyText", GetType(Control))> Public Class MyExtender
   <ExtenderProvidedProperty()> Public Function GetMyText(acontrol as _
      Control) as String
      ' Insert code to implement function.
   End Function
   Public Sub SetMytext (acontrol as Control)
      ' Insert code to implement function.
   End Function
End Class
using System.ComponentModel;
[ProvideProperty("MyText", typeof("Control"))]
public class MyExtender
{
[ExtenderProvidedProperty()]
   public string GetMyText(Control acontrol)
   {
      // Insert code to implement method.
   }
   public void SetMyText(Control acontrol)
   {
      // Insert code to implement method.
   }
}

The implementation of the provided property will require a Hashtable or some other such collection object to record and retrieve the property values for each control. For details, see How to: Implement an Extender Provider.

Every extender class must also implement the IExtenderProvider interface. This interface consists of a single method, CanExtend, which returns a Boolean value and indicates to the designer whether a component is a candidate to be extended or not. For example, you might want to create an extender that provides a property only to controls. The following example shows how to implement the CanExtend method:

Imports System.ComponentModel
Public Function CanExtend(ByVal extendee As Object) As Boolean _
   Implements IExtenderProvider.CanExtend
   If Typeof extendee Is Control Then
      Return True
   Else
      Return False
   End If
End Function
public bool CanExtend(object extendee) 
{
   if (extendee is Control)
      return true;
   else
      return false;
}

See Also

Tasks

How to: Implement an Extender Provider
How to: Implement a HelpLabel Extender Provider

Reference

IExtenderProvider
ProvidePropertyAttribute

Other Resources

Extender Providers