Click here to Skip to main content
6,306,412 members and growing! (18,150 online)
Email Password   helpLost your password?
Desktop Development » Smart Client » General     Intermediate

Customisable ErrorProviders

By Paul Stovell

A base class for customisable ErrorProviders that allow you to control how errors are shown
C# 2.0, Windows, .NET 2.0, WinForms, VS2005, Architect, Dev
Posted:2 Oct 2006
Views:15,265
Bookmarked:33 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
4 votes for this article.
Popularity: 2.56 Rating: 4.25 out of 5

1

2
1 vote, 25.0%
3
1 vote, 25.0%
4
2 votes, 50.0%
5

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 customise 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 to http://projectdistributor.net/Projects/Project.aspx?projectId=239.

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 focussed on the control. Setting this property to false supresses 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 :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Paul Stovell


Member
Paul Stovell is an Adelaide based developer working for Readify, a group of elite consultants specialising in technical readiness. Paul is a recognised expert on everything related to I.T., at least according to his mum. Paul speaks regularly at .NET usergroups around Australia, and keeps a website at www.paulstovell.com.
Occupation: Web Developer
Location: Australia Australia

Other popular Smart Client articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralThanks Paul! Pinmemberstephenpatten11:43 26 Jun '08  
GeneralHMM... PinmemberPITRep13:16 11 Jan '07  
GeneralRe: HMM... Pinmemberyannduran18:22 25 Feb '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Oct 2006
Editor:
Copyright 2006 by Paul Stovell
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project