ErrorProvider 클래스
업데이트: 2007년 11월
폼의 컨트롤에 관련된 오류가 있음을 나타내는 사용자 인터페이스를 제공합니다.
어셈블리: System.Windows.Forms(System.Windows.Forms.dll)
[ComplexBindingPropertiesAttribute("DataSource", "DataMember")] public class ErrorProvider : Component, IExtenderProvider, ISupportInitialize
/** @attribute ComplexBindingPropertiesAttribute("DataSource", "DataMember") */
public class ErrorProvider extends Component implements IExtenderProvider,
ISupportInitialize
public class ErrorProvider extends Component implements IExtenderProvider, ISupportInitialize
ErrorProvider는 폼의 컨트롤에 관련된 오류가 있음을 최종 사용자에게 알리는 간단한 메커니즘을 나타냅니다. 해당 컨트롤에 대한 오류를 설명하는 문자열이 컨트롤에 지정되면 컨트롤 옆에 아이콘이 표시되고, 해당 아이콘은 BlinkStyle에 지정된 방식과 BlinkRate에 지정된 속도로 깜박입니다. 마우스를 아이콘 위로 가져가면, 오류를 설명하는 문자열을 보여 주는 도구 설명이 표시됩니다.
일반적으로, ErrorProvider는 데이터 바인딩 컨트롤과 함께 사용합니다. ErrorProvider를 데이터 바인딩 컨트롤과 함께 사용할 때는, 생성자에서 ContainerControl을 지정하거나 ContainerControl 속성을 설정해야 합니다.
참고: |
|---|
ErrorProvider 구성 요소는 기본적으로 액세스 가능 클라이언트를 지원하지 않습니다. 이 구성 요소를 사용할 때 응용 프로그램에 액세스 가능 기능을 제공하려면 액세스 가능 피드백 메커니즘을 추가로 제공해야 합니다. |
다음 코드 예제에서는 ErrorProvider 클래스를 사용하여 사용자에게 데이터 입력 오류를 알리는 방법을 보여 줍니다. 다음 예제에서는 TextBox 컨트롤, NumericUpDown 컨트롤, ComboBox 컨트롤, 내용에 대한 각 유효성 검사 및 각 컨트롤에 대한 ErrorProvider가 포함된 Form을 만듭니다. 다음 예제에서는 BlinkRate 및 BlinkStyle 속성과 SetIconAlignment 및 SetIconPadding 메서드를 사용하여 오류 아이콘 옵션을 설정합니다. SetError 메서드는 컨트롤의 내용에 따라 컨트롤의 Validated 이벤트가 발생하는 동안 적절한 오류 텍스트를 포함하거나 포함하지 않은 상태로 호출됩니다.
using System; using System.Drawing; using System.Windows.Forms; namespace ErrorProvider { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox nameTextBox1; private System.Windows.Forms.NumericUpDown ageUpDownPicker; private System.Windows.Forms.ComboBox favoriteColorComboBox; private System.Windows.Forms.ErrorProvider ageErrorProvider; private System.Windows.Forms.ErrorProvider nameErrorProvider; private System.Windows.Forms.ErrorProvider favoriteColorErrorProvider; [STAThread] static void Main() { Application.Run(new Form1()); } public Form1() { this.nameTextBox1 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.ageUpDownPicker = new System.Windows.Forms.NumericUpDown(); this.favoriteColorComboBox = new System.Windows.Forms.ComboBox(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.label6 = new System.Windows.Forms.Label(); // Name Label this.label1.Location = new System.Drawing.Point(56, 32); this.label1.Size = new System.Drawing.Size(40, 23); this.label1.Text = "Name:"; // Age Label this.label2.Location = new System.Drawing.Point(40, 64); this.label2.Size = new System.Drawing.Size(56, 23); this.label2.Text = "Age (3-5)"; // Favorite Color Label this.label3.Location = new System.Drawing.Point(24, 96); this.label3.Size = new System.Drawing.Size(80, 24); this.label3.Text = "Favorite color"; // ErrorBlinkStyle.AlwaysBlink Label this.label4.Location = new System.Drawing.Point(264, 32); this.label4.Size = new System.Drawing.Size(160, 23); this.label4.Text = "ErrorBlinkStyle.AlwaysBlink"; // ErrorBlinkStyle.BlinkIfDifferentError Label this.label5.Location = new System.Drawing.Point(264, 64); this.label5.Size = new System.Drawing.Size(200, 23); this.label5.Text = "ErrorBlinkStyle.BlinkIfDifferentError"; // ErrorBlinkStyle.NeverBlink Label this.label6.Location = new System.Drawing.Point(264, 96); this.label6.Size = new System.Drawing.Size(200, 23); this.label6.Text = "ErrorBlinkStyle.NeverBlink"; // Name TextBox this.nameTextBox1.Location = new System.Drawing.Point(112, 32); this.nameTextBox1.Size = new System.Drawing.Size(120, 20); this.nameTextBox1.TabIndex = 0; this.nameTextBox1.Validated += new System.EventHandler(this.nameTextBox1_Validated); // Age NumericUpDown this.ageUpDownPicker.Location = new System.Drawing.Point(112, 64); this.ageUpDownPicker.Maximum = new System.Decimal(new int[] {150,0,0,0}); this.ageUpDownPicker.TabIndex = 4; this.ageUpDownPicker.Validated += new System.EventHandler(this.ageUpDownPicker_Validated); // Favorite Color ComboBox this.favoriteColorComboBox.Items.AddRange(new object[] {"None","Red","Yellow", "Green","Blue","Purple"}); this.favoriteColorComboBox.Location = new System.Drawing.Point(112, 96); this.favoriteColorComboBox.Size = new System.Drawing.Size(120, 21); this.favoriteColorComboBox.TabIndex = 5; this.favoriteColorComboBox.Validated += new System.EventHandler(this.favoriteColorComboBox_Validated); // Set up how the form should be displayed and add the controls to the form. this.ClientSize = new System.Drawing.Size(464, 150); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.label6,this.label5,this.label4,this.label3, this.favoriteColorComboBox,this.ageUpDownPicker, this.label2,this.label1,this.nameTextBox1}); this.Text = "Error Provider Example"; // Create and set the ErrorProvider for each data entry control. nameErrorProvider = new System.Windows.Forms.ErrorProvider(); nameErrorProvider.SetIconAlignment (this.nameTextBox1, ErrorIconAlignment.MiddleRight); nameErrorProvider.SetIconPadding (this.nameTextBox1, 2); nameErrorProvider.BlinkRate = 1000; nameErrorProvider.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink; ageErrorProvider = new System.Windows.Forms.ErrorProvider(); ageErrorProvider.SetIconAlignment (this.ageUpDownPicker, ErrorIconAlignment.MiddleRight); ageErrorProvider.SetIconPadding (this.ageUpDownPicker, 2); ageErrorProvider.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.BlinkIfDifferentError; favoriteColorErrorProvider = new System.Windows.Forms.ErrorProvider(); favoriteColorErrorProvider.SetIconAlignment (this.favoriteColorComboBox, ErrorIconAlignment.MiddleRight); favoriteColorErrorProvider.SetIconPadding (this.favoriteColorComboBox, 2); favoriteColorErrorProvider.BlinkRate = 1000; favoriteColorErrorProvider.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.NeverBlink; } private void nameTextBox1_Validated(object sender, System.EventArgs e) { if(IsNameValid()) { // Clear the error, if any, in the error provider. nameErrorProvider.SetError(this.nameTextBox1, ""); } else { // Set the error if the name is not valid. nameErrorProvider.SetError(this.nameTextBox1, "Name is required."); } } private void ageUpDownPicker_Validated(object sender, System.EventArgs e) { if (IsAgeTooYoung()) { // Set the error if the age is too young. ageErrorProvider.SetError(this.ageUpDownPicker, "Age not old enough"); } else if (IsAgeTooOld()) { // Set the error if the age is too old. ageErrorProvider.SetError(this.ageUpDownPicker, "Age is too old"); } else { // Clear the error, if any, in the error provider. ageErrorProvider.SetError(this.ageUpDownPicker, ""); } } private void favoriteColorComboBox_Validated(object sender, System.EventArgs e) { if (!IsColorValid()) { // Set the error if the favorite color is not valid. favoriteColorErrorProvider.SetError(this.favoriteColorComboBox, "Must select a color."); } else { // Clear the error, if any, in the error provider. favoriteColorErrorProvider.SetError(this.favoriteColorComboBox, ""); } } // Functions to verify data. private bool IsNameValid() { // Determine whether the text box contains a zero-length string. return (nameTextBox1.Text.Length > 0); } private bool IsAgeTooYoung() { // Determine whether the age value is less than three. return (ageUpDownPicker.Value < 3); } private bool IsAgeTooOld() { // Determine whether the age value is greater than five. return (ageUpDownPicker.Value > 5 ); } private bool IsColorValid() { // Determine whether the favorite color has a valid value. return ((favoriteColorComboBox.SelectedItem != null) && (!favoriteColorComboBox.SelectedItem.ToString().Equals("None"))); } } }
package ErrorProvider;
import System.*;
import System.Drawing.*;
import System.Windows.Forms.*;
public class Form1 extends System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox nameTextBox1;
private System.Windows.Forms.NumericUpDown ageUpDownPicker;
private System.Windows.Forms.ComboBox favoriteColorComboBox;
private System.Windows.Forms.ErrorProvider ageErrorProvider;
private System.Windows.Forms.ErrorProvider nameErrorProvider;
private System.Windows.Forms.ErrorProvider favoriteColorErrorProvider;
/** @attribute STAThread()
*/
public static void main(String[] args)
{
Application.Run(new Form1());
} //main
public Form1()
{
this.nameTextBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.ageUpDownPicker = new System.Windows.Forms.NumericUpDown();
this.favoriteColorComboBox = new System.Windows.Forms.ComboBox();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
// Name Label
this.label1.set_Location(new System.Drawing.Point(56, 32));
this.label1.set_Size(new System.Drawing.Size(40, 23));
this.label1.set_Text("Name:");
// Age Label
this.label2.set_Location(new System.Drawing.Point(40, 64));
this.label2.set_Size(new System.Drawing.Size(56, 23));
this.label2.set_Text("Age (3-5)");
// Favorite Color Label
this.label3.set_Location(new System.Drawing.Point(24, 96));
this.label3.set_Size(new System.Drawing.Size(80, 24));
this.label3.set_Text("Favorite color");
// ErrorBlinkStyle.AlwaysBlink Label
this.label4.set_Location(new System.Drawing.Point(264, 32));
this.label4.set_Size(new System.Drawing.Size(160, 23));
this.label4.set_Text("ErrorBlinkStyle.AlwaysBlink");
// ErrorBlinkStyle.BlinkIfDifferentError Label
this.label5.set_Location(new System.Drawing.Point(264, 64));
this.label5.set_Size(new System.Drawing.Size(200, 23));
this.label5.set_Text("ErrorBlinkStyle.BlinkIfDifferentError");
// ErrorBlinkStyle.NeverBlink Label
this.label6.set_Location(new System.Drawing.Point(264, 96));
this.label6.set_Size(new System.Drawing.Size(200, 23));
this.label6.set_Text("ErrorBlinkStyle.NeverBlink");
// Name TextBox
this.nameTextBox1.set_Location(new System.Drawing.Point(112, 32));
this.nameTextBox1.set_Size(new System.Drawing.Size(120, 20));
this.nameTextBox1.set_TabIndex(0);
this.nameTextBox1.add_Validated(new System.EventHandler(
this.nameTextBox1_Validated));
// Age NumericUpDown
this.ageUpDownPicker.set_Location(new System.Drawing.Point(112, 64));
this.ageUpDownPicker.set_Maximum(new System.Decimal(new int[] {
150, 0, 0, 0 }));
this.ageUpDownPicker.set_TabIndex(4);
this.ageUpDownPicker.add_Validated(new System.EventHandler(
this.ageUpDownPicker_Validated));
// Favorite Color ComboBox
this.favoriteColorComboBox.get_Items().AddRange(new Object[] {
"None", "Red", "Yellow", "Green", "Blue", "Purple" });
this.favoriteColorComboBox.set_Location(
new System.Drawing.Point(112, 96));
this.favoriteColorComboBox.set_Size(new System.Drawing.Size(120, 21));
this.favoriteColorComboBox.set_TabIndex(5);
this.favoriteColorComboBox.add_Validated(new System.EventHandler(
this.favoriteColorComboBox_Validated));
// Set up how the form should be displayed and add the controls to
// the form.
this.set_ClientSize(new System.Drawing.Size(464, 150));
this.get_Controls().AddRange(new System.Windows.Forms.Control[] {
this.label6, this.label5, this.label4, this.label3,
this.favoriteColorComboBox, this.ageUpDownPicker, this.label2,
this.label1, this.nameTextBox1 });
this.set_Text("Error Provider Example");
// Create and set the ErrorProvider for each data entry control.
nameErrorProvider = new System.Windows.Forms.ErrorProvider();
nameErrorProvider.SetIconAlignment(this.nameTextBox1,
ErrorIconAlignment.MiddleRight);
nameErrorProvider.SetIconPadding(this.nameTextBox1, 2);
nameErrorProvider.set_BlinkRate(1000);
nameErrorProvider.set_BlinkStyle(
System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink);
nameErrorProvider.set_ContainerControl(this);
ageErrorProvider = new System.Windows.Forms.ErrorProvider();
ageErrorProvider.SetIconAlignment(this.ageUpDownPicker,
ErrorIconAlignment.MiddleRight);
ageErrorProvider.SetIconPadding(this.ageUpDownPicker, 2);
ageErrorProvider.set_BlinkStyle(
System.Windows.Forms.ErrorBlinkStyle.BlinkIfDifferentError);
ageErrorProvider.set_ContainerControl(this);
favoriteColorErrorProvider = new System.Windows.Forms.ErrorProvider();
favoriteColorErrorProvider.SetIconAlignment(this.favoriteColorComboBox,
ErrorIconAlignment.MiddleRight);
favoriteColorErrorProvider.SetIconPadding(this.favoriteColorComboBox, 2);
favoriteColorErrorProvider.set_BlinkRate(1000);
favoriteColorErrorProvider.set_BlinkStyle(
System.Windows.Forms.ErrorBlinkStyle.NeverBlink);
favoriteColorErrorProvider.set_ContainerControl(this);
} //Form1
private void nameTextBox1_Validated(Object sender, System.EventArgs e)
{
if (IsNameValid()) {
// Clear the error, if any, in the error provider.
nameErrorProvider.SetError(this.nameTextBox1, "");
}
else {
// Set the error if the name is not valid.
nameErrorProvider.SetError(this.nameTextBox1, "Name is required.");
}
} //nameTextBox1_Validated
private void ageUpDownPicker_Validated(Object sender, System.EventArgs e)
{
if (IsAgeTooYoung()) {
// Set the error if the age is too young.
ageErrorProvider.SetError(this.ageUpDownPicker,
"Age not old enough");
}
else {
if (IsAgeTooOld()) {
// Set the error if the age is too old.
ageErrorProvider.SetError(this.ageUpDownPicker,
"Age is too old");
}
else {
// Clear the error, if any, in the error provider.
ageErrorProvider.SetError(this.ageUpDownPicker, "");
}
}
} //ageUpDownPicker_Validated
private void favoriteColorComboBox_Validated(Object sender,
System.EventArgs e)
{
if (!(IsColorValid())) {
// Set the error if the favorite color is not valid.
favoriteColorErrorProvider.SetError(this.favoriteColorComboBox,
"Must select a color.");
}
else {
// Clear the error, if any, in the error provider.
favoriteColorErrorProvider.SetError(
this.favoriteColorComboBox, "");
}
} //favoriteColorComboBox_Validated
// Functions to verify data.
private boolean IsNameValid()
{
// Determine whether the text box contains a zero-length string.
return ((nameTextBox1.get_Text().get_Length()) > 0);
} //IsNameValid
private boolean IsAgeTooYoung()
{
// Determine whether the age value is less than three.
return (System.Convert.ToInt32(ageUpDownPicker.get_Value()) < 3);
} //IsAgeTooYoung
private boolean IsAgeTooOld()
{
// Determine whether the age value is greater than five.
return (System.Convert.ToInt32(ageUpDownPicker.get_Value()) > 5);
} //IsAgeTooOld
private boolean IsColorValid()
{
// Determine whether the favorite color has a valid value.
return (favoriteColorComboBox.get_SelectedItem() != null)
&& (!(favoriteColorComboBox.get_SelectedItem().
ToString().Equals("None")));
} //IsColorValid
} //Form1
다음 코드 예제에서는 ErrorProvider와 DataSource 및 DataMember를 사용하여 사용자에게 데이터 오류를 나타내는 방법을 보여 줍니다.
private void InitializeComponent() { // Standard control setup. //.... // You set the DataSource to a data set, and the DataMember to a table. errorProvider1.DataSource = dataSet1 ; errorProvider1.DataMember = dataTable1.TableName ; errorProvider1.ContainerControl = this ; errorProvider1.BlinkRate = 200 ; //... // Since the ErrorProvider control does not have a visible component, // it does not need to be added to the form. } private void buttonSave_Click(object sender, System.EventArgs e) { // Checks for a bad post code. DataTable CustomersTable; CustomersTable = dataSet1.Tables["Customers"]; foreach (DataRow row in (CustomersTable.Rows)) { if (Convert.ToBoolean(row["PostalCodeIsNull"])) { row.RowError="The Customer details contain errors"; row.SetColumnError("PostalCode", "Postal Code required"); } } }
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.ErrorProvider
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework 및 .NET Compact Framework에서 모든 플랫폼의 전체 버전을 지원하지는 않습니다. 지원되는 버전의 목록을 보려면 .NET Framework 시스템 요구 사항을 참조하십시오.
참고: