The ExtraTag Property
The ExtraTagProvider class provides an ExtraTag
property for Components. This class performs three tasks:
- It indicates objects for which it may provide a property.
- It sets an object's property value.
- It retrieves an object's property value.
shows how the ExtraTagProvider class handles these chores. This code is in Visual Basic but you can also download a C# version
The class is decorated with a ProvideProperty attribute that indicates the name of the property that the class will provide: ExtraTag. The second parameter to the attribute indicates that the provider will serve Components, although the CanExtend function described shortly can refine that selection to indicate specific kinds of components that the class can extend .
The ExtraTagProvider class inherits from Component, so it appears in the Toolbox and can be placed on a form. It implements the IExtenderProvider interface.
IExtenderProvider requires a CanExtend function that takes an object as a parameter and returns True if the ExtenderProvider can serve that object. In this example, CanExtend returns True if the object is any kind of Component, but you could make this function much more specific. For example, you could make an ExtenderProvider that works for TextBoxes and Labels but not other controls. Or, you could make it work only for horizontal and vertical scroll bars and track bars, which all have similar Minimum, Maximum, LargeChange, SmallChange, and Value properties.
ExtraTagProvider must have some sort of data structure for storing the ExtraTag values for the Components it extends. This example uses a Dictionary instance named m_ExtraTags to store those values. The dictionary's keys are Components and its values are Strings. For a Component named C, m_ExtraTags(C) is the Component's ExtraTag value.
The GetExtraTag function returns a Componen's ExtraTag value. This function is decorated with the ExtenderProvidedProperty attribute. If the m_ExtraTags dictionary contains a value for this Component, the function returns it. If the dictionary doesn't contain an entry for this Component, the function returns an empty string.
The SetExtraTag subroutine saves a new ExtraTag value for a Component. It first removes the Component's entry in the m_ExtraTags dictionary if one is present. Then, if the new ExtraTag value is non-blank, it adds the new value to the dictionary.
Now you can avoid fighting with other developers over who has control of the Tag property. Add an ExtraTagProvider to a form and all of the form's Components get a new ExtraTag property that you can use while other, less enlightened developers wrangle over control of Tag.
In fact, other developers can also use an ExtraTagProvider to store their own information. If someone adds another ExtraTagProvider named ExtraTagProvider2 to the form, then every Component on the form gets another property named ExtraTag on ExtraTagProvider2 and each of you can use your ExtraTag properties without interfering with the other.
The form designer displays this property for components as ExtraTag on ExtraTagProvider1 and you can use the Properties window to set its value. However, ExtraTag is an extension provided by the ExtraTagProvider class, and is not truly a property of the components. That means your program's code can't set a component's ExtraTag property directly. Instead it must call the ExtraTagProvider's SetExtraTag method as shown in the following code.
ExtraTagProvider1.SetExtraTag(txtFirstName, "New value.")