I have a form with numerous textboxes and I use the validating event to validate these items.
If I want to close that form with the X button in the upper right corner of that window validation may not occur.
For example a user is filling in data and he inputs something incorrect in a field, but he then quickly wants to close the window by pushing X in the upper right corner.
At the moment validation goes off and show a dialogbox that incorrect data is enterred when I press X.
How can I turn validation off when pressing X, so that the window closes immediately without validation?
My initial thought was to set a global variable in the FormClosing event, but even with Application.DoEvents, there doesn't seem to be any way to get the FormClosing to fire before the Validation exits. You could use the Leave event instead of the Validating event because it does not fire when X'ing the form.
However, I think that a design change would be better: Add an ErrorProvider "control" to your form and set/clear the ErrorProvider flag in the Validating and/or Change event. Also, in that event, check to make sure that there are no controls with Errors, and enable the OK button only when all errors have been cleared.
http://www.c-sharpcorner.com/UploadF...andCSharp.aspx gives an example of this. It makes your form look much more professional and auto-magically gives error message. It was a very nice NET addition. HTH --Sam