views:

151

answers:

1

When creating an instance of a button within a .NET WinForms application, the .CausesValidation property is set to True. Why would all buttons be assumed to raise validation events? Doesn't this mean that, by default, all controls on a form with _Validating events will have that event called whenever the button simply gains focus?

Isn't gaining focus on a button a little early to call Validation events? Especially by default? The button click seems like a much more appropriate default time for validation to occur.

I ask because I'd like to be sure I am understanding the WinForms Validation pattern properly.

+2  A: 

I guess it is the safest of two evils. Often buttons process data; this way around, if you do nothing the default is that your data is validated, and it will be pretty obvious (since it doesn't work) if you didn't actually want it validating.

Contrast to the alternative - you do nothing, and your button silently works successfully performing actions on invalid data, and you don't notice because it is subtle.

The first is probably safer, even if it does cause a little irritation.

Marc Gravell