2

こんにちは、Visual Studio 2010 C# で大学のプロジェクトに取り組んでいます。8 つのテキスト ボックスを持つ WinForms アプリケーションがあります。ユーザーがテキスト ボックスを空のままにしておくと、エラー アイコンがポップアップ表示され、その横にラベルが表示され、エラー メッセージが表示されます。

以下のコードを実行すると、最初の 2 つのエラー プロバイダーのみが機能します。残りは表示されません。

誰でも私を助けることができますか?

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()代わりに、エラー プロバイダーのメソッドを使用してください。これにより、すべてのエラーがクリアされます。エラーを 1 つだけクリアしたい場合は、空のテキストを設定します。

そして、通常、フォームごとに必要なエラー プロバイダーは 1 つだけです。

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 に答える