Validation Integrated Windows Forms, Request option to prevent Cancel event in Control Validating event

Topics: Validation Application Block
Nov 4, 2010 at 7:13 AM
Edited Nov 4, 2010 at 7:15 AM

I'm try to implement windows application and using Validation Application block (Integrated Windows Form) and I have a few problem (not bug) when using ValidationProvider.
When user input invalid value into control that is set Validation (PerformValidation,SourcePropertyName,validatedProperty).
After user leave control (or try to enter another control), Validating event is fired and set e.Cancel = true and it focus invalid control (user can't enter another control except input valid value into control.).

Unfortunately my application allow user to enter another control even though control is input invalid value. I see the code in ValidationProvider.cs and ValidatedControlItem.cs and I revise your code for support my case. The code is shown in below
==========================================
1) ValidationProvider.cs
- Add new property bool EnabledCancelEventOnInvalid

public bool EnabledCancelEventOnInvalid { get; set; }

- At ValidatedControlItem EnsureValidatedControlItem(Control control) method
Original: Line 295
   existingItem = new ValidatedControlItem(this, control);

Change to:
    existingItem = new ValidatedControlItem(this, control,EnabledCancelEventOnInvalid);

================================================================
2) ValidatedControlItem.cs
- Add new proeprty bool EnabledCancelEventOnInvalid  (Same as ValidationProvider class)
  public bool EnabledCancelEventOnInvalid { get; set; }

- Add new constructor for support EnabledCancelEventOnInvalid argument

 public ValidatedControlItem(ValidationProvider validationProvider,
                                    Control control,bool enabledCancelEventOnInvalid) : this(validationProvider,control)
        {
               this.EnabledCancelEventOnInvalid  = enabledCancelEventOnInvalid;
         }

- Revise code at OnValidating(object,CancelEventArgs)
Original code:
               validationProvider.PerformValidation(this);
                e.Cancel = !IsValid;

Change to:
              validationProvider.PerformValidation(this);
              if (!EnabledCancelEventOnInvalid)
              {                 
                  e.Cancel = !IsValid;
               }

Finally. I request Enterprise Library team please provide an option to prevent Cancel event in Validating event when input invalid value into control and thank you so much to Enterprise Library team for build a great framework it make my happy in coding ^^.

Sittichok S.

Nov 4, 2010 at 7:25 AM
Edited Nov 4, 2010 at 7:27 AM

Let me see if I got it correctly:

Basically, you just want to be able to move to a different control even if you entered an invalid value to the current control? Is this right?  If this is what you want, you can just set the AutoValidate property of your windows form containing your error provider to EnableAllowFocusChange.

Does this provide answer to all of your concerns?  I'm not sure what do you mean in this statement:

"I request Enterprise Library team please provide an option to prevent Cancel event in Validating event when input invalid value into control"

 Could you state a scenario which explains this?

 

Sarah Urmeneta
Global Technologies & Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 4, 2010 at 8:00 AM

Thank you for your reply. I never know that set AutoValidate = EnableAllowFocusChange, It able to move different control event if I entered an invalid value to current control.

Q: Does this provide answer to all of your concerns?

A: Yes... that is my answer.

 

Sorry, If my request make you confuse. Thank you again for your answer.

 

Best Regard

Sittichok S.