Click here to Skip to main content
11,571,948 members (71,919 online)
Click here to Skip to main content

Validators for Windows Forms - ValidationProvider Control

, 16 Apr 2005 252.9K 9.1K 137
Rate this:
Please Sign up or sign in to vote.
Code-free validation for TextBox, ComboBox, and more...

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

Share

About the Author

Noogen
Web Developer
United States United States
If at first you don't succeed, refract and refract again...

You may also be interested in...

Comments and Discussions

 
QuestionIt is not work for me in VisualStudio 2010 Pin
nifox16-Oct-11 15:36
membernifox16-Oct-11 15:36 
AnswerRe: It is not work for me in VisualStudio 2010 Pin
sunmil9-Jan-12 16:29
membersunmil9-Jan-12 16:29 
Questioncustom validation Pin
Member 82998517-Oct-11 20:52
memberMember 82998517-Oct-11 20:52 
QuestionIt is not work for me in VisualStudio 2010 Pin
riovkiheller18-Sep-11 9:08
memberriovkiheller18-Sep-11 9:08 
AnswerRe: It is not work for me in VisualStudio 2010 Pin
nifox16-Oct-11 15:38
membernifox16-Oct-11 15:38 
QuestionError Pin
minhcanhdn2-Jul-11 5:53
memberminhcanhdn2-Jul-11 5:53 
GeneralVery cool, but how can it be used for controls in DataGridView? Pin
jaimepremy25-May-11 4:45
memberjaimepremy25-May-11 4:45 
GeneralThank you :) Pin
Technoshaman18-May-11 1:05
memberTechnoshaman18-May-11 1:05 
GeneralMultiple validation on a single control Pin
punit kumaar11-Feb-10 18:25
memberpunit kumaar11-Feb-10 18:25 
GeneralValidation Int64 value Pin
Mauro Gagna9-Dec-09 0:49
memberMauro Gagna9-Dec-09 0:49 
QuestionHow to Set Error Message Pin
aneeshkonny31-Mar-09 0:03
memberaneeshkonny31-Mar-09 0:03 
GeneralThe validation property grid don't show Pin
Joao Prado30-Jan-09 8:16
memberJoao Prado30-Jan-09 8:16 
GeneralAwesome! Pin
Ricky873-Sep-08 9:39
memberRicky873-Sep-08 9:39 
QuestionHow to use with Krypton Control Kit Pin
breakpoint2-Sep-08 18:41
memberbreakpoint2-Sep-08 18:41 
QuestionHow to add an option to use with other input controls? Pin
loverboy2319-Jul-08 7:39
memberloverboy2319-Jul-08 7:39 
GeneralI need some help Pin
Laco424-Mar-08 9:45
memberLaco424-Mar-08 9:45 
GeneralThank you Pin
Jecho Jekov27-Dec-07 10:48
memberJecho Jekov27-Dec-07 10:48 
GeneralFocus on First invalid Field Pin
Nic Roche12-Apr-07 15:19
memberNic Roche12-Apr-07 15:19 
GeneralRe: Focus on First invalid Field Pin
DiJ85-Jul-09 12:32
memberDiJ85-Jul-09 12:32 
GeneralRe: Focus on First invalid Field Pin
keshav120125-Mar-13 20:45
memberkeshav120125-Mar-13 20:45 
Questionmaskedtextbox Pin
pikey12328-Mar-07 9:31
memberpikey12328-Mar-07 9:31 
GeneralCustom Validations - help Pin
rontubi3-Dec-06 1:17
memberrontubi3-Dec-06 1:17 
GeneralOutstanding Pin
phimix27-Nov-06 23:33
memberphimix27-Nov-06 23:33 
QuestionDataTypeCheck and RequiredField Pin
kaelaa13-Nov-06 8:07
memberkaelaa13-Nov-06 8:07 
AnswerRe: DataTypeCheck and RequiredField Pin
youbest.cn10-Dec-06 14:27
memberyoubest.cn10-Dec-06 14:27 
GeneralRe: DataTypeCheck and RequiredField Pin
DiJ85-Jul-09 14:15
memberDiJ85-Jul-09 14:15 
QuestionMore than 1 Validation Rule per Control? Pin
Pink Floyd2-Nov-06 5:33
memberPink Floyd2-Nov-06 5:33 
GeneralMany thanks Pin
oz_scooter4-May-06 20:39
memberoz_scooter4-May-06 20:39 
Generalno regular expression validation if control is empty and not required Pin
Danny DB22-Mar-06 0:28
memberDanny DB22-Mar-06 0:28 
GeneralStrange problem on required currency textbox Pin
KjellSJ9-Mar-06 21:56
memberKjellSJ9-Mar-06 21:56 
GeneralWeird behavor on initial value Pin
aldofer4-Mar-06 6:23
memberaldofer4-Mar-06 6:23 
JokeAwesome Pin
OsirisFX2-Mar-06 10:28
memberOsirisFX2-Mar-06 10:28 
GeneralValidationProvider Control Pin
yunus demiray31-Jan-06 10:36
memberyunus demiray31-Jan-06 10:36 
GeneralRe: ValidationProvider Control Pin
Wiebe Tijsma16-Feb-06 6:50
memberWiebe Tijsma16-Feb-06 6:50 
GeneralRe: ValidationProvider Control Pin
Kerem Kat27-Jul-07 10:23
memberKerem Kat27-Jul-07 10:23 
JokeRe: ValidationProvider Control Pin
Jecho Jekov30-Nov-07 5:33
memberJecho Jekov30-Nov-07 5:33 
GeneralAwesome control Pin
JGraves25-Jan-06 7:33
memberJGraves25-Jan-06 7:33 
GeneralDateTimePicker Pin
Le_MuLoT21-Nov-05 10:42
memberLe_MuLoT21-Nov-05 10:42 
GeneralJust love it Pin
KjellSJ19-Nov-05 2:09
memberKjellSJ19-Nov-05 2:09 
GeneralInvalid input parameters Pin
hearthrob16-Nov-05 19:45
memberhearthrob16-Nov-05 19:45 
Generaltry to use it...but Pin
Jonte6-Oct-05 20:12
memberJonte6-Oct-05 20:12 
GeneralRe: try to use it...but Pin
Noogen8-Oct-05 3:22
memberNoogen8-Oct-05 3:22 
GeneralGood stuff! Pin
pauljenkins71421-Jun-05 10:59
memberpauljenkins71421-Jun-05 10:59 
GeneralRe: Good stuff! Pin
Noogen17-Jul-05 3:31
memberNoogen17-Jul-05 3:31 
QuestionHI, Pin
Amol Bhushan4-Jun-08 3:11
memberAmol Bhushan4-Jun-08 3:11 
GeneralSuggested Changes Pin
RAEdwards9-May-05 11:34
memberRAEdwards9-May-05 11:34 
GeneralRe: Suggested Changes Pin
Noogen17-Jul-05 3:27
memberNoogen17-Jul-05 3:27 
GeneralRe: Suggested Changes Pin
Le_MuLoT21-Nov-05 11:37
memberLe_MuLoT21-Nov-05 11:37 
GeneralYour work id really great. Pin
Narayan Ambatipudi27-Apr-05 4:46
memberNarayan Ambatipudi27-Apr-05 4:46 
GeneralCool! Pin
Kocha23-Apr-05 23:52
memberKocha23-Apr-05 23:52 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 16 Apr 2005
Article Copyright 2005 by Noogen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid