views:

88

answers:

2

Hi,

I've a winforms with some controls and an ErrorProvider. The control's Validating events are used to validate the data and report the error to the ErrorProvider if there is any. This work great when the user enter wrong informations into the control and leaves the control.

However, the validation does not happen if the user click on on without entering any data. Is there any way to force the validation of all controls when the user click on OK? And in the case where some validation failed, how would I know and cancel the OK event?

A: 

The Validating will be called when moving focus from a control on the dialog that has the CausesValidation property set to true to another control that has the CausesValidation property set to true.

Is the CausesValidation property on the OK button set to true?

Robert Harvey
+4  A: 

You can use Form.ValidateChildren to force the validation of all the controls on the form, as long as they support validation. Note that any control with CausesValidation set to False will not raise a Validating event and will therefore not be validated by ValidateChildren.

CodeByMoonlight
That's exactly what I was looking for. Thanks.
Mathieu Pagé
What about validating a single ComboBox? Form.ValidateChildren poses an efficiency problem when used on forms with a large number of controls that cause validation.
Rob