The ExtraTagProvider is handy on occasion, but it's really just another sneaky way to associate data with components. ExtenderProviders can do much more.
Because an ExtenderProvider works with instances of components, it can do all sorts of things to those components. For example, it can modify the components' properties and catch their events.
The FocusStatusLabelProvider (available for download in both Visual Basic and C# forms) is derived from the Label class. It is also an ExtenderProvider that provides the StatusMessage property. When an extended control receives focus, the FocusStatusLabelProvider displays the control's StatusMessage value. You can use this control to give users hints about what to enter in a field.
Much of the code that makes the class an ExtenderProvider, and that saves and restores properties, is similar to the code used by the ExtraTagProvider, but one difference is that FocusStatusLabelProvider inherits from the Label control instead of from Component.
A more interesting difference is that the SetStatusMessage subroutine adds and removes event handlers for the extended control. The following code shows how the SetStatusMessage subroutine works. If the m_StatusMessages dictionary contains a value for the extended control, the code removes its entry and any event handlers assigned to the control's GotFocus and LostFocus events. Then, if the new status message is non-blank, the routine adds it to the dictionary and attaches event handlers to the control's GotFocus and and LostFocus events. When an extendee receives focus, the provider sets its own text to display the extendee's status message. When an extendee loses focus, the provider clears its text:
Private Sub Extendee_GotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
Me.Text = GetStatusMessage(sender)
Private Sub Extendee_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
Me.Text = ""
shows the UseFocusStatusLabelProvider
application demonstrating this provider. In Figure 1
, the form's focus is in the Street TextBox
so the provider (docked to the bottom edge of the form) is displaying instructions for the Street
|Figure 1. FocusStatusLabelProvider: This class displays status information when a control has focus.|
A Missing Provider
The FocusStatusLabelProvider class catches its extendees' object's GotFocus and LostFocus events and displays appropriate messages. The MissingBackColorProvider class also catches extendees' events and takes action.
This class catches a TextBox's Validating event and determines whether the control contains text. If the control is blank, the provider saves the TextBox's current BackColor value in its m_BackColor dictionary and then sets the control's BackColor to the value stored in the extender's MissingBackColor property. If the TextBox contains text, the provider restores its original BackColor.
The MissingBackColorProvider makes it easy to flag a required field with a colored background if the user leaves it blank (download the Visual Basic and C# programs that demonstrate this provider here.)