首页 > 解决方案 > 有没有办法只为取消按钮覆盖验证事件?

问题描述

我试图为 ID 文本框添加一个验证事件,以确保在提交表单时,用户必须填写 ID 文本框。表单还有一个取消按钮来关闭表单。

关闭按钮执行此操作this.Close();

但现在问题是即使我单击取消按钮,验证事件也在执行。我想要一种方法来解决这个问题。

下面是代码。

//This is the code for my Cancel button
private void btnCancel_Click(object sender, EventArgs e)
{
   CausesValidation = false;
   this.Close();
}

//This is the code for my Validating event
private void tbID_Validating(object sender, CancelEventArgs e)
{
   /*if Supplier name and Company name filled user can't go forward without filling Supplier ID*/
   if (string.IsNullOrEmpty(tbID.Text))
   {
      e.Cancel = true;
      errorProviderID.SetError(tbID, "You can not leave supplier ID empty");
   }
   else
   {
      errorProviderID.Clear();
   }
}

CausesValidation 已设置为 false

标签: c#winforms

解决方案


推荐阅读