How to prevent validating from being fired when I click X (Close button) on the form

前端 未结 3 1463
礼貌的吻别
礼貌的吻别 2020-12-20 05:38

This is a winform C# question. I have a textbox with a validating event listener to validate the content of the textbox. Say, the textbox doesn\'t accept negative values. I

相关标签:
3条回答
  • 2020-12-20 06:01

    Hmmm, the only way I would think to do it is to set AutoValidate on the form to false and handle validation in the controls manually. The form has .Validate() and .ValidateChildren() methods, read up on these as they are what you need to perform validation. To handle it manually, you will need to listen for when a control is losing focus - if validation fails you then need to perhaps re-focus the offending control.

    Alternatively, make the form ControlBox = false; to remove the X button.

    Update: Alternatively again you can use a member variable to test whether to validate or not (ie, whether the form is closing or not). You cannot do this using the FormClosing event as this fires after the validation, however you can detect form closing via WndProc. Code is provided in this post:

    http://technoblot.wordpress.com/2006/09/08/winforms-causesvalidation-not-working-a-workaround/

    A slightly less involved workaround.

    0 讨论(0)
  • 2020-12-20 06:07

    I solved this issue by setting the form CausesValidation property to False. The form now closes normally when I click the X button.

    0 讨论(0)
  • 2020-12-20 06:17

    When closing the form, the form checks if the AutoValidate property of the form has a value different from Disabled then it raises the Validating event of the focused control. Also if there is any validation error (by setting e.Cancel = true in the Validating event), it prevents closing of the form.

    To prevent raising the Validating error when closing the form, you need to override WndProc and before processing WM_CLOSE message set AutoValidate to Disabled and after that, set it back to original value. The reason for setting it back to original value is because you may want to prevent closing the form and then it's expected the value of AutoValidate be the same value that the developer has been set originally:

    private const int WM_CLOSE = 0x0010;
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_CLOSE)
        {
            var autoValidate = this.AutoValidate;
            this.AutoValidate = AutoValidate.Disable;
            base.WndProc(ref m);
            this.AutoValidate = autoValidate;
        }
        else
            base.WndProc(ref m);
    }
    

    Note: If you just want to allow the form get closed after click on close button, it's enough to override OnClosing method and set e.Cancel = false after calling base.OnClose. But it doesn't prevent the Validating event. It just allows the form get closed even if there is a validation error in the focused control.

    You can read more about the problem in this post: Prevent raising of Validating event of focused control when Closing the form.

    0 讨论(0)
提交回复
热议问题