2014-01-07 10 views
2

안녕하세요 저는 Visual Studio 2010 C#에서 대학 프로젝트를 진행하고 있습니다. 8 개의 텍스트 상자가있는 WinForms 응용 프로그램이 있습니다. 사용자가 텍스트 상자를 비워 둘 때마다 오류 아이콘이 표시되고 오류 아이콘을 표시하는 레이블이 표시되어야합니다.모든 errorproviders가 제대로 작동하지 않습니다.

아래 코드를 실행하면 처음 두 오류 공급자 만 작동합니다. 나머지는 나타나지 않습니다.

아무도 도와 줄 수 있습니까?

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-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); 
     } 
    } 
} 
+1

해결 방법을 시도했지만 나에게 도움이되지 못했습니다. 대체 솔루션을 원하십니까 ?? ... 노력 neway 감사합니다 – Brian

+0

좀 더 구체적으로 말하십시오. 무슨 일을 했지? 컴파일 오류? 런타임 에러? 원하는 효과가 아닙니까? 모든 errorprovider 컨트롤을 하나로 변경하고 모두 이름을 변경 했습니까? 나는 단지 하나의 이름을 바꾸는 것을 보여 줬다. – okrumnow

+0

didnt는 원하는 효과를 얻는다. 모든 errorproviders에 string.empty 속성을 별도로 사용했습니다. – Brian