Validation on form close

Wizard for Windows Forms Forum

Posted 18 years ago by Alex Mollison
Avatar
Hi, is there anyway to stop the validation happening when the form is close either by clicking the Cancel button or clicking on the X button.

I am using VB 2005 and the latest version of the wizard control

Comments (4)

Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
When I run our sample in VS 2002 and press Cancel, it closes the Form fine. When I run our sample in VS 2005 and press Cancel, it is doing the validation and blocking the close.

By default we set the Cancel button's CausesValidation to false. The code we kick in to force validation doesn't occur for the Cancel button click unless you set CancelButtonCausesValidation to true.

I'm open to any other suggestions, or is this simply a nasty bug in the .NET 2.0 framework? I'm thinking it's a bug of sorts in the .NET 2.0 framework since it works fine in pre-.NET 2.0 versions.


Actipro Software Support

Posted 18 years ago by Alex Mollison
Avatar
Thanks for the answer. I have tried setting the Wizard.CausesValidation property to False in the FormClosing but the validation event must fire first. Could you add the FormClosing or a similar event so that fires before validation fires?
Posted 18 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
The only things we do validation-related are these:
1) Give you access to the CausesValidation properties of each of the buttons. By default Back, Cancel, and Help have CausesValidation set to false, while Next and Finish have it set to true.
2) We force a validation of the current page when a button is pressed only if the button's CausesValidation is set to true.
3) We default the WizardPage.CausesValidation property setting to false (since item #2 kicks in the validation upon button clicks).

So as you can see, we aren't doing anything tricky here with validation. After debugging this some more, I found a workaround.

If you make a control invalid and press the Next button, the Next button's click event won't fire. This is correct behavior since its CausesValidation is true. If you make a control invalid and press the Cancel button, the Cancel button's click event DOES fire. This is also correct behavior since its CausesValidation is false.

What happens is that the Cancel button click event handler calls this.Close and something in the Form's internal close code is kicking off the validation. Then Microsoft's code is raising the Form.Closing event with the Cancel property defaulting to true, whereas in .NET 1.x it was always false. So what you can do to handle this is change that Cancel property back to false and it will close like it should.

It seems like Microsoft added this as a "feature" probably to prevent you from closing a form if there is invalid data on it.


Actipro Software Support

Posted 18 years ago by Alex Mollison
Avatar
I got it to work that after the message box that shows the validation problem (just as you would see it clicking the next button), the program closes after dismissing the dialog. I set Canel = False on the FormClosing event.
Thanks for your help
The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.