Click here to Skip to main content
Licence CPOL
First Posted 2 Oct 2006
Views 23,632
Downloads 377
Bookmarked 48 times

Customizable ErrorProviders

By | 2 Oct 2006 | Article
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)

About the Author

Paul Stovell

Software Developer
Readify
Australia Australia

Member

Follow on Twitter Follow on Twitter
My name is Paul Stovell, and I help customers to architect and develop applications on the Windows platform. I work for Readify, an Australian-based consulting company. I am a Microsoft MVP for Client Application Development, and you'll often find me hanging out at user groups and conferences.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks Paul! Pinmemberstephenpatten10:43 26 Jun '08  
GeneralHMM... PinmemberPITRep12:16 11 Jan '07  
This is exactly what i was looking for, I was unsure of how to implement a Error Provider like the default built in one but to show what I want and how I want it to.
 
Question Though:
In the Customer Object you implement INotifyPropertyChange but don't ever actually implemet it. At least how Microsoft does here:http://msdn2.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.propertychanged.aspx
 
Just wondering why?
GeneralRe: HMM... Pinmemberyannduran17:22 25 Feb '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 2 Oct 2006
Article Copyright 2006 by Paul Stovell
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid