Validation Block - Winform Integration - Focus Management

Topics: Building and extending application blocks, Validation Application Block
Apr 1, 2010 at 2:48 PM
Edited Apr 1, 2010 at 3:07 PM

Hi Guys,

Currently the Validation Block winforms integration will automatically cancel any validation if it fails, preventing progress to the next item of the form or changing focus. I've come across a number of scenarios where I would like the user to be able to proceed, just having the errors displayed, and progress prevented on clicking OK or Submit, but this is not possible with the current configuration as focus change is prevented. Also - the cancel button cannot be clicked while validation fails, which feels very klunky.

I have made some changes myself but would like to submit them as a feature request so that they can be used with the pre-compiled and installed blocks.


In the ValidationProvider, add a new property (Defaulted to false):

///Flag indicating whether Focus should be 
/// prevented when validation fails
bool PreventRefocus

Then, in the ValidatedControlItem, where the base control Validated event is handled, change the OnValidating method as follows:

OnValidating(object source, CancelEventArgs e) { if (PerformValidation) { validationProvider.PerformValidation(this); e.Cancel = (validationProvider.PreventRefocus && !IsValid) } }
This simple change will enable you to switch between holding up progress on control validation, and allowing it to continue,
simply by setting a flag in the validation provider.

Any thoughts On this approach?


Tristan Rhodes
Apr 5, 2010 at 7:26 AM

I think your requirement can be achieved without modifying the source code.  Simply set the AutoValidate property of your windows form containing your error provider to EnableAllowFocusChange.

"Also - the cancel button cannot be clicked while validation fails, which feels very klunky"

You can resolve this by making sure that the Perform Validation on <validationProviderName> is set to false or just simply set the CausesValidation property to False.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.