The Windows Forms
ErrorProvider control and the
IDataErrorInfo interface provide a very powerful way to automatically validate objects and display errors. However, you may find the way that the built-in
ErrorProvider displays errors to be somewhat limited. For example, rather than showing the built-in flashy red icon, you might prefer to show a
MessageBox, or to change the background colors of invalid controls. This sample project provides a
SmartErrorProviderBase class that allows you to hook in to the validation system and customize the way error messages are displayed.
Note 1: If you haven't used the Windows Forms
ErrorProvider or the
IDataErrorInfo interface, refer to my previous article on Delegates and Business Objects.
Note 2: The code attached to this article is the current version at the time of writing. Any updates will be uploaded here.
SmartErrorProviderBase class implements the
IExtenderProvider interface, and is designed to be subclassed before using. It provides you with two pieces of functionality:
- It raises a
BindingValidated event that you can subscribe to in order to control how to display the error message.
- It provides a
ShowErrorsImmediately property. Sometimes, you may not want to display error messages until the user has actually focused on the control. Setting this property to
false suppresses any error messages until the user has tabbed away from the control.
When you drag the
SmartErrorProviderBase or one of its derivatives onto your form, there are two properties you will need to set. The first is the
DataSource, which is expected to be the same class that your UI controls are bound to, and should implement the
System.ComponentModel.IDataErrorInfo interface. The second is the
ContainerControl property. This is simply used so that the Error Provider can find all of the bindings on your form, and should almost always be set to your
Form. Somehow the built-in
ErrorProvider manages to set this automatically, but I can't figure out how to do this.
I have included two sample implementations in the sample code, although it's expected that most of you would extend these to suit your own requirements. The two samples are:
ErrorProvider simply changes the background color of any invalid controls. You can specify a single error color (red by default) as a property on the
BackgroundColorErrorProvider, or you can set colors for individual controls thanks to the
ErrorBackColor property which is provided by the
This is actually a
UserControl which contains a
SmartErrorProviderBase as well as a
ListView, and uses the
ListView to display error messages. When you click on one of the error messages, the focus changes to the control that is invalid.
I hope that the code is pretty self-explanatory. If there are a lot of requests for the same type of
ErrorProvider display, I'll consider adding it to sample code. Note that this was a rough implementation, and while it seems to work fine, there may be bugs hidden deep that I haven't come across, so use these classes at your own risk.
- 2nd October, 2006: Initial post