ErrorProvider控件中禁用验证时,点击取消按钮
-
22-09-2019 - |
题
有一个方法来禁用ErrorProvider控件的验证典雅当单击取消按钮以关闭一个winform? 验证总是发生在文本框失去焦点,我也不当用户点击取消按钮想它来验证,它仅仅是一个有点傻,以验证当用户点击取消。
解决方案
谷歌搜索后,找到了答案,的取消按钮,以假刚刚集合CauseValidation属性。就是这样。
其他提示
我只是遇到这个自己和设置CauseValidation = false
只是部分解决方案。
在设置Form.CancelButton
到取消按钮,Escape键应该调用该按钮。它,然而,验证仍然运行响应Escape键,即使我们设置CauseValidation = false
。
要解决它,添加以下劈:
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);
}
不隶属于 StackOverflow