C# Custom ErrorProvider

Download ErrorProvider.zip (4 KB)

In this weeks post I’ve extended the standard ErrorProvider class with a few features I find really handy:

ClearError Method
An overload for the SetError method, passing String.Empty as the value. Its always annoyed me that in order to “Clear” an error, I had to “Set” the error to an empty string.

Count Property
Returns the number of controls that currently have an error (without iterating over a collection).

Controls Property
Returns a Dictionary object containing all controls that currently have errors, and their respective error messages.

Here’s an example of how to iterate over all current error messages:

CodeOverload.Windows.ErrorProvider errorProvider = new CodeOverload.Windows.ErrorProvider();
// ...validation logic goes here... //
Dictionary<Control, string> controls = errorProvider.Controls;
foreach (KeyValuePair<Control, string> error in controls)
{
  Control errorControl = error.Key;
  string errorMessage = error.Value;
}

Full source code is available (C# VS2010):
Download ErrorProvider.zip (4 KB)

Advertisements
This entry was posted in Projects and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s