Integrating with Windows Forms-based applications

The Validation Application Block provides integration with Windows Forms applications and validates user input. The ValidationProvider component part of the Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WinForms assembly is an extender provider that adds additional properties to Windows Forms controls. Validation can be performed by using the Control.Validating event or it can be invoked manually in our code using the ValidationProvider.PerformValidation(Control) method. Additionally, it provides integration with Windows Forms ErrorProvider component to display visual indication to the user of the error.

Note

We must add reference to the Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WinForms.dll to leverage the integration features.

Steps to leverage ValidationProvider

  1. Add ValidationProvider to the Toolbox.
  2. Right-click on the Toolbox and click Choose Items... menu, this will load the Choose Toolbox Items dialog. In the .NET Framework Components tab, select the ValidationProvider component.
    Steps to leverage ValidationProvider
    Steps to leverage ValidationProvider
  3. Add ErrorProvider and a ValidationProvider component to the Windows Forms.
    Steps to leverage ValidationProvider
  4. Configure ValidationProvider by selecting the ErrorProvider component and by assigning the SourceTypeName to the fully qualified name of the type to be validated. Optionally, RulesetName can be configured to use a specific Rule Set; also the component can be enabled or disabled by setting the Enabled property.
    Steps to leverage ValidationProvider
  5. Configure controls for validation.
  6. Assuming we want to validate the Author class, which consists of First Name, Last Name, and Email ID, since ValidationProvider adds additional properties to the controls, we can configure SourcePropertyName to the respective property name in the Author class. ValidatedProperty is set to Text by default for a TextBox control and PerformValidation is set to True by default.
    Steps to leverage ValidationProvider
  7. The following screenshot shows the extended properties that have to be configured. SourcePropertyName determines the property to be used of the type configured in the ValidationProvider.
    Steps to leverage ValidationProvider
  8. Perform validation either using ValidateChildren or the ValidationProvider.PerformValidation(Control) method.
  9. The following code snippet shows the validation call using the Windows Forms ValidateChildren method:
    this.ValidateChildren(ValidationConstraints.Visible);
    
  10. The following code snippet shows the validation call using the ValidationProvider:
    validationProviderAuthor.PerformValidation(textBoxFirstName);
    
    Steps to leverage ValidationProvider
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.9.148