2

嗨,我正在使用 Visual Studio 2010 C# 进行我的大学项目。我有一个 WinForms 应用程序,它有 8 个文本框。每当用户将文本框留空时,应该会弹出错误图标,并且应该在其旁边显示一个标签以显示错误消息。

当我执行下面的代码时,只有前两个错误提供程序起作用。其余的不显示。

有人可以帮我吗?

private void textBox1_Leave(object sender, EventArgs e)
{
    if (String.IsNullOrEmpty(textBox1.Text))
    {
        errorProvider1.SetError(textBox1,"REQUIRED FIELD");
        label12.Text = "REQUIRED FIELD";
    }
    else
    {
        errorProvider1.Dispose();
    }
 }

 private void textBox2_Leave(object sender, EventArgs e)
 {
     monthCalendar1.Visible = false;
     if (String.IsNullOrEmpty(textBox2.Text))
     {
         errorProvider2.SetError(textBox2,"REQUIRED FIELD");
         label13.Text = "REQUIRED FIELD";
     }
     else
     {
         errorProvider2.Dispose();
     }
 }

 private void textBox3_Leave(object sender, EventArgs e)
 {

     if (textBox3.Text=="")
     {
         errorProvider3.SetError(textBox3, "REQUIRED FIELD");
         label14.Text = "REQUIRED FIELD";
     }
     else
     {
         errorProvider3.Dispose();
     }
  }

  private void textBox4_Leave(object sender, EventArgs e)
  {
      monthCalendar1.Visible = false;
      if (String.IsNullOrEmpty(textBox4.Text))
      {
          errorProvider4.SetError(textBox4, "REQUIRED FIELD");
          label15.Text = "REQUIRED FIELD";
      }
      else
      {
          errorProvider4.SetError(textBox4, "");
      }
  }

  private void textBox5_Leave(object sender, EventArgs e)
  {

      if (String.IsNullOrEmpty(textBox5.Text))
      {
          errorProvider5.SetError(textBox5, "REQUIRED FIELD");
          label16.Text = "REQUIRED FIELD";
      }
      else
      {
          errorProvider5.SetError(textBox5, "");
      }
  }

  private void textBox6_Leave(object sender, EventArgs e)
  {
      monthCalendar2.Visible = false;
      if (String.IsNullOrEmpty(textBox6.Text))
      {
          errorProvider6.SetError(textBox6, "REQUIRED FIELD");
          label17.Text = "REQUIRED FIELD";
      }
      else
      {
          errorProvider6.SetError(textBox6, "");
      }
  }
4

1 回答 1

2

每当有人在文本框中输入文本时,您就处置您的错误提供程序。释放对象会释放所有资源并防止进一步使用。

请改用Clear()错误提供程序的方法。这将清除所有错误。如果您只想清除一个错误,请设置一个空文本。

而且 - 通常 - 每个表单只需要一个错误提供程序。

private void textBox1_Leave(object sender, EventArgs e)
{
    if (String.IsNullOrEmpty(textBox1.Text))
    {
                     errorProvider.SetError(textBox1,"REQUIRED FIELD");
                     label12.Text = "REQUIRED FIELD";
    }
    else
    {
        errorProvider.SetError(textBox1, String.Empty); // to clear only the error for this text box
        // errorProvider.Clear(); // to clear all errors for this provider
    }
}

编辑:提供了完整的工作示例

这简化为处理错误提供程序。每当光标离开文本字段时,都会检查该字段的内容。如果为空,则显示错误。用户需要返回该字段,输入dta并再次离开以清除错误。这基本上就是您在示例中定义为要求的内容。我没有切换标签文本,但这似乎不是问题。

要签出,请创建一个新的 WinForm-Application 并用此代码替换 Form1 类。为简洁起见,我将InitialiseComponent()代码包含在构造函数中,因此 VS 可能不会显示表单(至少 VS2010 无法正确显示)。

public partial class Form1 : Form
{
    public Form1()
    {
        this.components = new System.ComponentModel.Container();
        this.errorProvider1 = new ErrorProvider(this.components);
        this.textBox1 = new TextBox();
        this.textBox2 = new TextBox();
        ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).BeginInit();
        this.SuspendLayout();
        // 
        // errorProvider1
        // 
        this.errorProvider1.ContainerControl = this;
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(42, 25);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(100, 20);
        this.textBox1.TabIndex = 0;
        this.textBox1.Leave += this.textBox1_Leave;
        // 
        // textBox2
        // 
        this.textBox2.Location = new System.Drawing.Point(42, 52);
        this.textBox2.Name = "textBox2";
        this.textBox2.Size = new System.Drawing.Size(100, 20);
        this.textBox2.TabIndex = 1;
        this.textBox2.Leave += this.textBox2_Leave;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.textBox2);
        this.Controls.Add(this.textBox1);
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();
    }

    private void textBox1_Leave(object sender, System.EventArgs e)
    {
        if (string.IsNullOrEmpty(textBox1.Text)) {
            errorProvider1.SetError(textBox1, "REQUIRED FIELD");
        }
        else {
            errorProvider1.SetError(textBox1, string.Empty);
        }
    }

    private void textBox2_Leave(object sender, System.EventArgs e)
    {
        if (string.IsNullOrEmpty(textBox2.Text))
        {
            errorProvider1.SetError(textBox2, "REQUIRED FIELD");
        }
        else
        {
            errorProvider1.SetError(textBox2, string.Empty);
        }
    }
}
于 2014-01-07T15:03:23.833 回答