我在关闭表单时遇到了问题,因为我的ErrorProvider不允许我这样做。为了纠正它,我做了曾傑瑞操纵,创建了一个变量Boolena,以确认表单是否被关闭。我已经尝试过FormClosing,但是ErrorProvider仍然在验证焦点字段。有人也有同样的问题吗?你是怎么解决的?我想做得更优雅些。
表单初始化
public partial class MyForm: form
{
private bool _isClosing = false;
...
...
}错误提供程序泛型验证
private bool ValidatingFields(Control control)
{
bool eventArgs = false;
if (!this._isClosing) //Boolean Variable jerry-rigged
{
if (string.IsNullOrEmpty(control.Text))
{
eventArgs = true;
control.BackColor = Color.Red;
errorProvider.SetError(control, "Campo Obrigatório!");
}
else
{
eventArgs = false;
errorProvider.SetError(control, null);
}
}
return eventArgs;
}字段验证
private void textName_Validating(object sender, CancelEventArgs e)
{
e.Cancel = ValidatingFields(textName);
}事件关闭按钮单击
private void btnCancel_Click(object sender, EventArgs e)
{
this._isClosing = true;
this.Close();
}发布于 2021-05-29 16:05:09
是啊,这种行为没什么不对的。控件验证将触发除CausesValidation = False以外的任何按钮单击事件。简而言之,您可以将cancel按钮的这个属性设置为"False“。这将跳过验证。
https://stackoverflow.com/questions/67753162
复制相似问题