Validation form integration - Validation block tutorial 4/4
And now this is the last part of the tutorial. In this section I would like to show you how to put your Validation code into the windows form application. When you finish to combine your validation code and your windows form, the windows form will perform automatic validation when user type on your input form.

I will show you how to do this?
Using the previous example about validating User and Address object, now we create the integration form for those object.
1. You need to include the Validation.Integration.WinForms project to your project. In your Microsoft visual studio right click in your solution then choose add > existing project then Point to your Enterprise Library home > App Blocks > Src > Validation > Integration > WinForms. Choose the Validation.Integration.WinForms.csproj then click open.

You can see now in your toolbox a new tab called Validation.Integration.WinForms Components.Add the ValidationProvider control in your form.
After you do this, right click your project and choose add reference. On the Add reference dialog choose project tab then add the ValidationIntegrationWinForms in the list provided.

Next, you have to add another control called ErrorProvider into your windows form.

2. The second step is about to configure each control that you have been add to your windows form. Choose the ValidationProvider control in your form then open the properties window. Change the default name of ValidationProvider control to your desire name. Then choose ErrorProvider properties to the one in your form. After that fill the SourceTypeName with the full name of your class to validate followed by ‘,’ and your Project name. the complete configuration of ValidationProvider will look like something like this.

3. Create your input field based on the properties on the User and Address class. Then configure each of the field properties. Bellow is the properties that have to be configured to use the ValidationProvider.
- PerformValidation on <Your ValidationProvider name>, set to be true.
- SourcePropertyName on <Your ValidationProvider name>, fill it with one of the properties in your Object to validate.
- ValidatedProperty on <Your ValidationProvider name>, fill it with field property that hold the value of it.
Here is the picture about how to do it.

After you finish to configure each field, run the application and doing some test case.
Click here to download the complete code of this example.
Sometimes there is an error when you try to validate using PropertyComparisonValidator. It’s show a message “ValidationProvider.GetExistingValidatedControlItem throws NullReferenceException”. Read this discussion in the forum about how to fix it.
Leave a Reply