views:

20

answers:

1

I have two form classes inheriting from a common base. One of the forms is called modally and the other non-modally. Validation is required on focus changes but not when the form is cancelled. When the Close Box is selected on the modal form it closes properly without any validation being triggered on it's controls. When the Close Box is selected on the non-modal form, validation events are triggered. A Cancel button with CausesValidation set false works fine in both cases.

I have tried setting CausesValidation on the non-modal form to false but the problem remains. I should mention that the forms are mdi children.

Any ideas?

Thx.

+1  A: 

Set the "AutoValidate" property of the non-modal form to "Disable". This will prevent the Form from implicitly validating all child controls.

However, at a later time if you require to validate all child controls you can use Form.ValidateChildren() method. That will force validation for all contained child controls.

Rajarshi
Thank-you. That worked. Still not sure why a non-modal form Close Box operates differently than a modal one wrt validation. But time to move on.
Governor
That's by design. Referring following content from MSDN topic titled "User Input Validation in Windows Forms" - ".... In addition, modal forms do not validate the contents of controls when they are closed. You can still use control validation to lock focus to a control, but you do not have to be concerned about the behavior associated with closing the form...."
Rajarshi
This answer worked great for me, especially since the validation still occurred automatically when I expected it (e.g. when a `TextBox` gained and then lost focus).
Pat