preventing validation in OnClosing

  • Thread starter Thread starter Alex Bink
  • Start date Start date
A

Alex Bink

Hi,

Anyone knows how to prevent the validating event of a control to be fired
when the user clicks the close button? The OnClosing event of the form seems
to be to late and setting the form's CausesValidation property to false
doesn't seem to help either...

Thanks...
 
Alex,

The only suggestion I've seen from Microsoft is to go through your
entire form in your OnClosing event, setting CausesValidation on every
control to false. I consider this completely lame.

I suggest creating a bool flag in your form to act as a "don't bother
validating" flag:

private bool dontBotherValidating = false;

then every _Validating event handler looks like this:

private void aControl_Validating(object sender, System.EventArgs e)
{
if (!dontBotherValidating)
{
.... do validation stuff here ...
}
}

then, in OnClosing, you can add as the first line:

this.dontBotherValidating = true;

I know it's not pretty, but I have never found a way to flush pending
Validating events... you seem to have no choice but to have them fire,
so I just accepted that fact and handled the problem within my
Validating event handlers.
 
Back
Top