首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FormClosing治疗ErrorProvider

FormClosing治疗ErrorProvider
EN

Stack Overflow用户
提问于 2021-05-29 15:35:03
回答 1查看 60关注 0票数 1

我在关闭表单时遇到了问题,因为我的ErrorProvider不允许我这样做。为了纠正它,我做了曾傑瑞操纵,创建了一个变量Boolena,以确认表单是否被关闭。我已经尝试过FormClosing,但是ErrorProvider仍然在验证焦点字段。有人也有同样的问题吗?你是怎么解决的?我想做得更优雅些。

表单初始化

代码语言:javascript
复制
public partial class MyForm: form
{
  private bool _isClosing = false;
...
...
}

错误提供程序泛型验证

代码语言:javascript
复制
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;
}

字段验证

代码语言:javascript
复制
private void textName_Validating(object sender, CancelEventArgs e)
{
  e.Cancel = ValidatingFields(textName);
}

事件关闭按钮单击

代码语言:javascript
复制
private void btnCancel_Click(object sender, EventArgs e)
{
  this._isClosing = true;
  this.Close();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-29 16:05:09

是啊,这种行为没什么不对的。控件验证将触发除CausesValidation = False以外的任何按钮单击事件。简而言之,您可以将cancel按钮的这个属性设置为"False“。这将跳过验证。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67753162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档