Validation and Close / Cancel in WinForms

I added a error provider in a form and added some validation logic. But, when I want to cancel and close the form, the form doesn’t close until the error is fixed. The way to fix this to set the Cancel property of the form closing event to be false.

How about if I have a cancel button and would like the same behavior as above? The trick now is to set the CausesValidation property of the cancel button to be false. Now, if you try this and it still doesn’t work, then just make sure that all the (parent) containers, excluding the form, of the cancel button also have the CausesValidation property as false.

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name (required)

 Email (will not be published) (required)

 Website

Your comment is 0 characters limited to 3000 characters.