Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent that a event in a control trigger when the form is closing

Tags:

c#

.net

winforms

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?

like image 982
José Gordillo López Avatar asked Jul 01 '13 19:07

José Gordillo López


1 Answers

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);
}
like image 153
Mike Perrenoud Avatar answered Sep 21 '22 22:09

Mike Perrenoud