Make an extender provider that validates required TextBoxes in C#

An extender provider provides properties and services for other controls and components on a form. For example, the ToolTip component is an extender provider. When you add one to a form, the other controls on the form get a new "ToolTip on ToolTip1" property that is provided by the ToolTip.

Extender providers are an under-used feature of C#, possibly because of the strange way you implement them. You can build your own to do all sorts of things. For example, an extender provider can store information for controls or catch control events and do something. This example shows how an extender provider can validate a TextBox's value and, if it is blank, give the TextBox a distinctive background color so the user can see that the value is required.

The basic steps for building an ExtenderProvider are:
  1. Create a new component (select the Project menu's Add Component command).
  2. Make the component inherit the IExtenderProvider interface. Add it to the class statement as in:
    public partial class RequiredFieldsChecker : Component, IExtenderProvider
  3. Write the CanExtend function required by the IExtenderProvider interface. This function takes an object as a parameter and returns true if it can extend that object. This example uses the following code:
    // We can extend TextBoxes only.
    public bool CanExtend(object extendee)
    {
    return (extendee is TextBox);
    }
  4. Give the component a ProvideProperty attribute to tell Visual Basic the names of the properties that it will provide. This example uses the following code:
    [ProvideProperty("MissingBackColor", "System.Windows.Forms.TextBox")]
    public partial class RequiredFieldsChecker : Component, IExtenderProvider
  5. Build get and set routines for those properties. These routines must have the same names as the properties with the words Get and Set added to the front.

This example uses the following code to implement its MissingBackColor property.

// The list of our clients and their colors.
private List<TextBox> Clients =
    new List<TextBox>();
private Dictionary<TextBox, Color> MissingColors =
    new Dictionary<TextBox, Color>();
private Dictionary<TextBox, Color> BackColors =
    new Dictionary<TextBox, Color>();

// Implement the MissingBackColor extension property.
// Return this client's MissingBackColor value.
[Category("Appearance")]
[DefaultValue(null)]
public Color? GetMissingBackColor(TextBox client)
{
// Return the control's MissingBackColor if it exists.
if (MissingColors.ContainsKey(client)) return MissingColors[client];
return null;
}

// Set this control's FocusTip.
[Category("Appearance")]
[DefaultValue(null)]
public void SetMissingBackColor(TextBox client, Color? missing_back_color)
{
// Get this control's ClientInfo.
if (missing_back_color.HasValue)
{
// Save the client's missing color.
MissingColors[client] = missing_back_color.Value;
Clients.Add(client);

// Catch the client's Validating event.
client.Validating -= Client_Validating;
client.Validating += Client_Validating;
}
else
{
// If the client is in our client list, remove it.
if (MissingColors.ContainsKey(client))
{
Clients.Remove(client);
MissingColors.Remove(client);

// No longer receive the client's Validating event.
client.Validating -= Client_Validating;
}
}
}

// Display the MissingBackColor if appropriate.
private void Client_Validating(object sender, CancelEventArgs e)
{
ValidateClient(sender as TextBox);
}
private void ValidateClient(TextBox client)
{
if (client.Text.Length < 1)
{
BackColors[client] = client.BackColor;
client.BackColor = MissingColors[client];
}
else
{
if (BackColors.ContainsKey(client))
{
client.BackColor = BackColors[client];
BackColors.Remove(client);
}
}
}

The extender provider uses a List and two Dictionaries to hold its client TextBoxes and their MissingBackColor and BackColor values. When the control validates a client and finds that it is blank, it saves the client's BackColor value and sets BackColor to the value stored in the MissingColors Dictionary. If the user enters a value, the extender provider resets the BackColor to the control's original BackColor value.

The GetMissingBackColor and SetMissingBackColor methods implement the MissingBackColor property for the clients. GetMissingBackColor simply returns the value stored in the MissingColors Dictionary.

SetMissingBackColor checks whether the new value is null indicating that the TextBox should not be required. If the value is null, the code removes the client from the List and Dictionaries. If the value is not null, the code sets or updates the value.

When the code adds a TextBox to the client list, it also registers the Client_Validating method to receive the client's Validating event. When the event fires, the program calls ValidateClient to give the control the appropriate BackColor.

Note that the program could catch the control's TextChanged event to make update the control's BackColor whenever the value changes. You can decide which method is more appropriate.

The extender provider also provides a couple of methods that the main program can use.

// Return true if a required field is blank.
public TextBox FirstMissingField()
{
// Check all of the fields.
CheckAllFields();

// See if any clients are blank.
foreach (TextBox client in Clients)
{
if (client.Text.Length < 1) return client;
}
return null;
}

// Check all clients now. This is useful
// for initializing background colors.
public void CheckAllFields()
{
foreach (TextBox client in Clients)
{
ValidateClient(client);
}
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.