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. This ErrorProvider simply changes the background color of any invalid controls. The two samples are: BackgroundColorErrorProvider 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. Somehow the built-in ErrorProvider manages to set this automatically, but I can't figure out how to do this. 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. The second is the ContainerControl property. The first is the DataSource, which is expected to be the same class that your UI controls are bound to, and should implement the interface. ![]() When you drag the SmartErrorProviderBase or one of its derivatives onto your form, there are two properties you will need to set. Setting this property to false suppresses any error messages until the user has tabbed away from the control. Sometimes, you may not want to display error messages until the user has actually focused on the control. It provides a ShowErrorsImmediately property.It raises a BindingValidated event that you can subscribe to in order to control how to display the error message.It provides you with two pieces of functionality: The SmartErrorProviderBase class implements the IExtenderProvider interface, and is designed to be subclassed before using. ![]() Note 2: The code attached to this article is the current version at the time of writing. 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. 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. 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. However, you may find the way that the built-in ErrorProvider displays errors to be somewhat limited. The Windows Forms ErrorProvider control and the IDataErrorInfo interface provide a very powerful way to automatically validate objects and display errors.
0 Comments
Leave a Reply. |