Click here to Skip to main content
6,292,426 members and growing! (9,893 online)
Email Password   helpLost your password?
Web Development » Validation » Validation Controls     Intermediate

Validators for Windows Forms - ValidationProvider Control

By Noogen

Code-free validation for TextBox, ComboBox, and more...
C#, VB, Windows, .NET 1.1, WinForms, VS.NET2003, Architect, DBA, Dev, QA
Posted:11 Apr 2005
Updated:16 Apr 2005
Views:87,190
Bookmarked:78 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
48 votes for this article.
Popularity: 7.40 Rating: 4.40 out of 5

1
2 votes, 4.3%
2
3 votes, 6.4%
3
5 votes, 10.6%
4
37 votes, 78.7%
5

Introduction

Do you spend most of your time writing custom validations on Windows Forms? Have you ever wanted an equivalent WinForm validator like in WebForm? Noogen ValidationProvider can help you speed up your WinForms design time by providing powerful, easy and virtually code-free wiring of input validations.

Background

For the beginners, I would suggest a reading of Paul Riley's article on Validators in ASP.NET/WebForms - ASP.NET Validators Unclouded. This is because ValidationProvider engine is derived from WebForm Validators base.

Noogen ValidationProvider is also built on ErrorProvider, see the article by Naveen K. Kohli - How To Use The ErrorProvider Object To Indicate Invalid Control State.

I know that this topic has been discussed many times (example MSDN article - Extending Windows Forms with a Custom Validation Component Library, Part 3), but most of the articles I've seen are either too restricted to a single control (TextBox) or too complicated. My goal with Noogen ValidationProvider is to bring all these great ideas together and simplify them.

The design

  1. To speed up development, Noogen ValidationProvider re-uses WebForm validator base engine and displays error using ErrorProvider Control (hence the name ValidationProvider).
  2. My second goal is to provide easy wiring of basic/familiar validations as seen in WebForm, which includes: DataTypes check, required field, range and value comparison validations, and more...
  3. Noogen ValidationProvider also tests and validates regular expressions and can even wire more advance custom validations.

I'll stop here before I go too far off from the topic. Of course, if you wish to drill down on the details, all the source codes are provided.

Using the code

Basic Validations

ValidationProvider makes it easy to setup validation for TextBox and ComboBox controls. You don't have to write a single line of code. Just compile the accompanied source in release mode, browse and add it to your VS.NET IDE ToolBox, drop it on the form, and you are ready to visually design your input validations.

ValidationProvider property exposes Edit ValidationRules link/verb on the "Properties Window". I have also implemented IExtenderProvider to enable easy editing of specific ValidationRule ties to individual TextBox or ComboBox controls.

The only code you will need is given below:

//

// Call Noogen ValidationProvider public method - 

// Validate() and display error if returns false.

//

this.validationProvider1.ValidationMessages(
                      !this.validationProvider1.Validate());

Public Methods

CanExtend Determine if the ValidationProvider supports a component.
GetIconAlignment Get Error Icon alignment.
GetIconPadding Get Error Icon padding.
GetValidationRule Get validation rule.
SetIconAlignment Set Error Icon alignment.
SetIconPadding Set Error Icon padding.
SetValidationRule Set validation rule.
Validate Overloaded. Perform validation on all controls.
ValidationMessages Get validation error messages.

Regular expression validations

ValidationProvider can test and validate regular expressions (RegEx). For your convenience, a RegEx test bench is provided. RegEx test value applies the same validation engine, and the data gets validated as you type in. You can wire some very powerful and advance validations with RegEx. With creativity, I believe that you can virtually use it in place of all the Basic Validations, but RegEx can be complicated to derive. Thus, you would probably use RegEx validation as an accommodation to the provided Basic Validations. The demo application demonstrates simple email and data length Regular Expression validations.

If you've worked with regular expressions before, then you would have probably collected some favorite RegEx patterns. ValidationProvider allows you to load your collection RegEx patterns so that you can re-use it consistently and share it with your developer friends. You would simply edit and add your RegEx collection to the accompanied file "RegExPatternStore.xml" as seen in the animation above.

Custom Validations

OK, I lied! The above code is not the only code you will need. When business logic gets complicated or if it is required to validate controls that the ValidationProvider does not support, custom validation methods can be wired onto ValidationProvider like this:

//

// Create new ValidationRule and wire-up custom validation method.  

// This can be somewhere in the ctor.

//

Noogen.Validation.ValidationRule vr = new Noogen.Validation.ValidationRule();
vr.CustomValidationMethod += 
 new Noogen.Validation.CustomValidationEventHandler(vr_CustomValidationMethod);
this.validationProvider1.SetValidationRule(this.textBox6, vr);
//

// The custom validation method.

//

private void vr_CustomValidationMethod(object sender, 
                 Noogen.Validation.CustomValidationEventArgs e)
{
   e.IsValid = 
        e.Value.ToString().Equals("abc") || e.Value.ToString().Equals("123") ;
   e.ErrorMessage = "%ControlName% is not in ('abc', '123').";
}

Point of interest

For the purpose of demonstration, I have put both the Noogen.Validation.Design and ValidationProvider in a single project. You would probably want to name your build of ValidationProvider to separate the Designer from the Provider. This will reduce the assembly size, since the Designer is not required for deployment.

History

  • April 11th, 2005 - ValidationProvider 1.0 posted.

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

Noogen


Member
If at first you don't succeed, refract and refract again...
Occupation: Web Developer
Location: United States United States

Other popular Validation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 38 (Total in Forum: 38) (Refresh)FirstPrevNext
GeneralHow to Set Error Message Pinmemberaneeshkonny1:03 31 Mar '09  
GeneralThe validation property grid don't show PinmemberJoao Prado9:16 30 Jan '09  
GeneralAwesome! PinmemberRicky8710:39 3 Sep '08  
GeneralHow to use with Krypton Control Kit Pinmemberbreakpoint19:41 2 Sep '08  
GeneralHow to add an option to use with other input controls? Pinmemberloverboy238:39 19 Jul '08  
GeneralI need some help PinmemberLaco4210:45 4 Mar '08  
GeneralThank you PinmemberJecho Jekov11:48 27 Dec '07  
GeneralFocus on First invalid Field PinmemberNic Roche16:19 12 Apr '07  
Questionmaskedtextbox Pinmemberpikey12310:31 28 Mar '07  
GeneralCustom Validations - help Pinmemberrontubi2:17 3 Dec '06  
GeneralOutstanding Pinmemberphimix0:33 28 Nov '06  
QuestionDataTypeCheck and RequiredField Pinmemberkaelaa9:07 13 Nov '06  
AnswerRe: DataTypeCheck and RequiredField Pinmemberyoubest.cn15:27 10 Dec '06  
GeneralMore than 1 Validation Rule per Control? PinmemberPink Floyd6:33 2 Nov '06  
GeneralMany thanks Pinmemberoz_scooter21:39 4 May '06  
Generalno regular expression validation if control is empty and not required PinmemberDanny DB1:28 22 Mar '06  
GeneralStrange problem on required currency textbox PinmemberKjellSJ22:56 9 Mar '06  
GeneralWeird behavor on initial value Pinmemberaldofer7:23 4 Mar '06  
JokeAwesome PinmemberOsirisFX11:28 2 Mar '06  
GeneralValidationProvider Control Pinmemberyunus demiray11:36 31 Jan '06  
GeneralRe: ValidationProvider Control PinmemberWiebe Tijsma7:50 16 Feb '06  
GeneralRe: ValidationProvider Control PinmemberKerem Kat11:23 27 Jul '07  
JokeRe: ValidationProvider Control PinmemberJecho Jekov6:33 30 Nov '07  
GeneralAwesome control PinmemberJGraves8:33 25 Jan '06  
GeneralDateTimePicker PinmemberLe_MuLoT11:42 21 Nov '05  

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

PermaLink | Privacy | Terms of Use
Last Updated: 16 Apr 2005
Editor: Rinish Biju
Copyright 2005 by Noogen
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project