单击取消按钮时禁用错误提供者的验证

发布于 2024-08-17 01:01:52 字数 114 浏览 7 评论 0原文

当单击“取消”按钮关闭 winform 时,有没有办法优雅地禁用 errorprovider 的验证? 当文本框失去焦点时,验证总是发生,我不希望它在用户单击取消按钮时进行验证,当用户单击取消时进行验证有点愚蠢。

is there a way to disable the validation of errorprovider elegantly when click cancel button to dismiss a winform?
The validation always happens when the textbox lose focus, and i don't wanna it to validate when the user click cancel button, it is just a little bit silly to validate when the user clicking cancel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两仪 2024-08-24 01:01:52

谷歌搜索后,找到了答案,只需将取消按钮的 CauseValidation 属性设置为 false 即可。就是这样。

after googling, found the answer, just set CauseValidation property of the cancel button to false. that's it.

雄赳赳气昂昂 2024-08-24 01:01:52

我自己刚刚遇到了这个问题,设置 CauseValidation = false 只是部分解决方案。

当您将 Form.CancelButton 设置为取消按钮时,Escape 键应该调用该按钮。然而,即使我们设置了CauseValidation = false,验证仍然会运行以响应Escape 键。

要修复它,请添加以下 hack:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    // Although we set CausesValidation = false for the Cancel button,
    //  the Escape key fails to cancel due to validation failure. The
    //  Form.CancelButton property should invoke the validation-free
    //  cancel button, but doesn't. Force the issue here.
    if (keyData == Keys.Escape)
    {
        DialogResult = DialogResult.Cancel;
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

I just ran into this myself and setting CauseValidation = false is only a partial solution.

When you set the Form.CancelButton to the cancel button, the Escape key is supposed to invoke that button. It does, however, validation still runs in response to the Escape key, even though we set CauseValidation = false.

To fix it, add the following hack:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    // Although we set CausesValidation = false for the Cancel button,
    //  the Escape key fails to cancel due to validation failure. The
    //  Form.CancelButton property should invoke the validation-free
    //  cancel button, but doesn't. Force the issue here.
    if (keyData == Keys.Escape)
    {
        DialogResult = DialogResult.Cancel;
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文