C validating close form dating fender stratocaster guitar
However, if the failing validation is preventing the cancel button from doing its thing, set the for the cancel button and all the parents of the cancel button. It seemed that if the user was in the middle of editing a field that was using validation and just decided to give up (leaving the field with an invalid input), the cancel button event was being fired but the window would not close down.
This was fixed by the following in the cancel button click event: Setting Causes Validation to false is the key, however this alone is not enough.
// The flag private bool _is Closing = false; // Action that avoids validation protected override void On Closing(Cancel Event Args e) // Validated event handler private void txt Control To Validate_Validated(object sender, Event Args e) // Do validation on complete so you'll remain on same thread void worker_Run Worker Completed(object sender, Run Worker Completed Event Args e) // Give a delay, I'm not sure this is necessary cause I tried to remove the Thread. void worker_Do Work(object sender, Do Work Event Args e) This is an old question however I recently ran into this issue and solved it this way: 1st, we are loading a User Control into a 'shell' Form that has a save and cancel button.
I have a windows form that validates if there is text in a text box, which works well when going through the rest of the form, however, if the user just wants to click the close(x) button in the upper right corner instead of actually running the application, I get the exception handling message of text not being in the text box instead of the application exiting.
I looked around here and looked at other questions in regards to detecting the close button being pushed and that led me to trying the below.
I want to add validation to the Text Box which will prevent the user from OK-ing the form when validation fails, but which will also allow them to cancel as usual.
The Causes Validation property is True by default on all the controls, but I have changed this to False on the Cancel Button.