2014-01-07 38 views
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, ""); 
     } 
    } 

回答

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,应用以及与此代码替换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); 
     } 
    } 
} 
+1

尝试了您的soltion但它没有工作了我。你有任何替代解决方案??感谢努力neway – Brian

+0

更具体。什么没有奏效?编译错误?运行时错误?不是期望的效果?你是否已将所有错误提供者控件更改为一个,并将它们全部更名?我只显示了一个重命名... – okrumnow

+0

没有得到预期的效果。我用所有的errorproviders分别 – Brian