How to prevent an event in a control trigger when the form closes

advertisements

My problem is that I validate a TextBox in the Leave event and I check that this TextBox has something. If the text in this TextBox is the same to zero a MessageBox appears, but if the form is closing and the focus is in this TextBox the MessageBox appears because the TextBox doesn't have anything.

How can avoid the validate in the Leave event when the form is closing?


You will need to override the OnFormClosing method on the form and set a flag letting you know that the form is closing. Then, when validating, check that flag first.

Be sure to set the flag before calling base.

protected override void OnFormClosing(FormClosingEventArgs e)
{
    _isClosing = true;

    base.OnFormClosing(e);
}