Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / Windows Forms

Customizable ErrorProviders

Rate me:
Please Sign up or sign in to vote.
4.24/5 (5 votes)
2 Oct 2006CPOL3 min read 47K   1.3K   54   4
A base class for customizable ErrorProviders that allow you to control how errors are shown
Sample Image - CustomErrorProviders.png

Introduction

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.

Features

The SmartErrorProviderBase class implements the IExtenderProvider interface, and is designed to be subclassed before using. It provides you with two pieces of functionality:

  1. It raises a BindingValidated event that you can subscribe to in order to control how to display the error message.
  2. 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.

Samples

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:

BackgroundColorErrorProvider

This 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 BackgroundColorErrorProvider IExtenderProvider implementation.

ErrorMessageList

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.

Enjoy!

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. :)

History

  • 2nd October, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Octopus Deploy
Australia Australia
My name is Paul Stovell. I live in Brisbane and develop an automated release management product, Octopus Deploy. Prior to working on Octopus I worked for an investment bank in London, and for Readify. I also work on a few open source projects. I am a Microsoft MVP for Client Application Development.

Comments and Discussions

 
GeneralThanks Paul! Pin
stephenpatten26-Jun-08 10:43
stephenpatten26-Jun-08 10:43 
GeneralHMM... Pin
PITRep11-Jan-07 12:16
PITRep11-Jan-07 12:16 
GeneralRe: HMM... Pin
yannduran25-Feb-09 17:22
yannduran25-Feb-09 17:22 
GeneralHelp Pin
Vantuil21-Aug-13 8:42
Vantuil21-Aug-13 8:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.