单击取消按钮以关闭winform时,有没有办法优雅地禁用errorprovider的验证?验证总是在文本框失去焦点时发生,我不希望它在用户单击取消按钮时进行验证,当用户单击取消时进行验证有点愚蠢。
问问题
5402 次
2 回答
15
谷歌搜索后,找到了答案,只需将取消按钮的 CauseValidation 属性设置为 false。就是这样。
于 2010-01-03T15:20:29.187 回答
6
我自己也遇到了这个问题,设置CauseValidation = false
只是部分解决方案。
当您将 设置Form.CancelButton
为取消按钮时,Escape 键应该调用该按钮。但是,它确实会响应 Escape 键运行验证,即使我们设置了CauseValidation = false
.
要修复它,请添加以下 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);
}
于 2011-04-25T16:01:55.203 回答