ErrorProvider Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menyediakan antarmuka pengguna untuk menunjukkan bahwa kontrol pada formulir memiliki kesalahan yang terkait dengannya.
public ref class ErrorProvider : System::ComponentModel::Component, System::ComponentModel::IExtenderProvider
public ref class ErrorProvider : System::ComponentModel::Component, System::ComponentModel::IExtenderProvider, System::ComponentModel::ISupportInitialize
public class ErrorProvider : System.ComponentModel.Component, System.ComponentModel.IExtenderProvider
[System.ComponentModel.ComplexBindingProperties("DataSource", "DataMember")]
public class ErrorProvider : System.ComponentModel.Component, System.ComponentModel.IExtenderProvider, System.ComponentModel.ISupportInitialize
type ErrorProvider = class
inherit Component
interface IExtenderProvider
[<System.ComponentModel.ComplexBindingProperties("DataSource", "DataMember")>]
type ErrorProvider = class
inherit Component
interface IExtenderProvider
interface ISupportInitialize
Public Class ErrorProvider
Inherits Component
Implements IExtenderProvider
Public Class ErrorProvider
Inherits Component
Implements IExtenderProvider, ISupportInitialize
- Warisan
- Atribut
- Penerapan
Contoh
Contoh kode berikut menunjukkan penggunaan ErrorProvider kelas untuk memberi tahu pengguna tentang kesalahan entri data. Contoh membuat Form yang berisi TextBox kontrol, NumericUpDown kontrol, dan ComboBox kontrol, masing-masing memvalidasi kontennya, dan ErrorProvider untuk setiap kontrol. Contoh menetapkan opsi ikon kesalahan menggunakan BlinkRate properti dan BlinkStyle dan SetIconAlignment metode dan SetIconPadding . Metode SetError ini dipanggil dengan atau tanpa teks kesalahan yang sesuai selama peristiwa kontrol Validated , tergantung pada konten dalam kontrol.
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
public ref class Form1: public System::Windows::Forms::Form
{
private:
System::Windows::Forms::Label ^ label1;
System::Windows::Forms::Label ^ label2;
System::Windows::Forms::Label ^ label4;
System::Windows::Forms::Label ^ label5;
System::Windows::Forms::Label ^ label6;
System::Windows::Forms::Label ^ label3;
System::Windows::Forms::TextBox^ nameTextBox1;
System::Windows::Forms::NumericUpDown^ ageUpDownPicker;
System::Windows::Forms::ComboBox^ favoriteColorComboBox;
System::Windows::Forms::ErrorProvider^ ageErrorProvider;
System::Windows::Forms::ErrorProvider^ nameErrorProvider;
System::Windows::Forms::ErrorProvider^ favoriteColorErrorProvider;
public:
Form1()
{
this->nameTextBox1 = gcnew System::Windows::Forms::TextBox;
this->label1 = gcnew System::Windows::Forms::Label;
this->label2 = gcnew System::Windows::Forms::Label;
this->ageUpDownPicker = gcnew System::Windows::Forms::NumericUpDown;
this->favoriteColorComboBox = gcnew System::Windows::Forms::ComboBox;
this->label3 = gcnew System::Windows::Forms::Label;
this->label4 = gcnew System::Windows::Forms::Label;
this->label5 = gcnew System::Windows::Forms::Label;
this->label6 = gcnew System::Windows::Forms::Label;
// Name Label
this->label1->Location = System::Drawing::Point( 56, 32 );
this->label1->Size = System::Drawing::Size( 40, 23 );
this->label1->Text = "Name:";
// Age Label
this->label2->Location = System::Drawing::Point( 40, 64 );
this->label2->Size = System::Drawing::Size( 56, 23 );
this->label2->Text = "Age (3-5)";
// Favorite Color Label
this->label3->Location = System::Drawing::Point( 24, 96 );
this->label3->Size = System::Drawing::Size( 80, 24 );
this->label3->Text = "Favorite color";
// ErrorBlinkStyle::AlwaysBlink Label
this->label4->Location = System::Drawing::Point( 264, 32 );
this->label4->Size = System::Drawing::Size( 160, 23 );
this->label4->Text = "ErrorBlinkStyle::AlwaysBlink";
// ErrorBlinkStyle::BlinkIfDifferentError Label
this->label5->Location = System::Drawing::Point( 264, 64 );
this->label5->Size = System::Drawing::Size( 200, 23 );
this->label5->Text = "ErrorBlinkStyle::BlinkIfDifferentError";
// ErrorBlinkStyle::NeverBlink Label
this->label6->Location = System::Drawing::Point( 264, 96 );
this->label6->Size = System::Drawing::Size( 200, 23 );
this->label6->Text = "ErrorBlinkStyle::NeverBlink";
// Name TextBox
this->nameTextBox1->Location = System::Drawing::Point( 112, 32 );
this->nameTextBox1->Size = System::Drawing::Size( 120, 20 );
this->nameTextBox1->TabIndex = 0;
this->nameTextBox1->Validated += gcnew System::EventHandler( this, &Form1::nameTextBox1_Validated );
// Age NumericUpDown
this->ageUpDownPicker->Location = System::Drawing::Point( 112, 64 );
array<int>^temp0 = {150,0,0,0};
this->ageUpDownPicker->Maximum = System::Decimal( temp0 );
this->ageUpDownPicker->TabIndex = 4;
this->ageUpDownPicker->Validated += gcnew System::EventHandler( this, &Form1::ageUpDownPicker_Validated );
// Favorite Color ComboBox
array<Object^>^temp1 = {"None","Red","Yellow","Green","Blue","Purple"};
this->favoriteColorComboBox->Items->AddRange( temp1 );
this->favoriteColorComboBox->Location = System::Drawing::Point( 112, 96 );
this->favoriteColorComboBox->Size = System::Drawing::Size( 120, 21 );
this->favoriteColorComboBox->TabIndex = 5;
this->favoriteColorComboBox->Validated += gcnew System::EventHandler( this, &Form1::favoriteColorComboBox_Validated );
// Set up how the form should be displayed and add the controls to the form.
this->ClientSize = System::Drawing::Size( 464, 150 );
array<System::Windows::Forms::Control^>^temp2 = {this->label6,this->label5,this->label4,this->label3,this->favoriteColorComboBox,this->ageUpDownPicker,this->label2,this->label1,this->nameTextBox1};
this->Controls->AddRange( temp2 );
this->Text = "Error Provider Example";
// Create and set the ErrorProvider for each data entry control.
nameErrorProvider = gcnew 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 = gcnew System::Windows::Forms::ErrorProvider;
ageErrorProvider->SetIconAlignment( this->ageUpDownPicker, ErrorIconAlignment::MiddleRight );
ageErrorProvider->SetIconPadding( this->ageUpDownPicker, 2 );
ageErrorProvider->BlinkStyle = System::Windows::Forms::ErrorBlinkStyle::BlinkIfDifferentError;
favoriteColorErrorProvider = gcnew 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, String.Empty );
}
else
{
// Set the error if the name is not valid.
nameErrorProvider->SetError( this->nameTextBox1, "Name is required." );
}
}
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, String.Empty );
}
}
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, String.Empty );
}
}
// Functions to verify data.
bool IsNameValid()
{
// Determine whether the text box contains a zero-length String*.
return (nameTextBox1->Text->Length > 0);
}
bool IsAgeTooYoung()
{
// Determine whether the age value is less than three.
return (ageUpDownPicker->Value < 3);
}
bool IsAgeTooOld()
{
// Determine whether the age value is greater than five.
return (ageUpDownPicker->Value > 5);
}
bool IsColorValid()
{
// Determine whether the favorite color has a valid value.
return ((favoriteColorComboBox->SelectedItem != 0) && ( !favoriteColorComboBox->SelectedItem->Equals( "None" )));
}
};
[STAThread]
int main()
{
Application::Run( gcnew Form1 );
}
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, String.Empty);
}
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, String.Empty);
}
}
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, String.Empty);
}
}
// 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")));
}
}
}
Imports System.Drawing
Imports System.Windows.Forms
Namespace ErrorProvider
Public NotInheritable Class Form1
Inherits System.Windows.Forms.Form
Private label1 As System.Windows.Forms.Label
Private label2 As System.Windows.Forms.Label
Private label3 As System.Windows.Forms.Label
Private label4 As System.Windows.Forms.Label
Private label5 As System.Windows.Forms.Label
Private label6 As System.Windows.Forms.Label
Friend WithEvents favoriteColorComboBox As System.Windows.Forms.ComboBox
Friend WithEvents nameTextBox1 As System.Windows.Forms.TextBox
Friend WithEvents ageUpDownPicker As System.Windows.Forms.NumericUpDown
Private ageErrorProvider As System.Windows.Forms.ErrorProvider
Private nameErrorProvider As System.Windows.Forms.ErrorProvider
Private favoriteColorErrorProvider As System.Windows.Forms.ErrorProvider
<System.STAThread()> _
Public Shared Sub Main()
System.Windows.Forms.Application.Run(New Form1())
End Sub
Public Sub New()
Me.nameTextBox1 = New System.Windows.Forms.TextBox()
Me.label1 = New System.Windows.Forms.Label()
Me.label2 = New System.Windows.Forms.Label()
Me.ageUpDownPicker = New System.Windows.Forms.NumericUpDown()
Me.favoriteColorComboBox = New System.Windows.Forms.ComboBox()
Me.label3 = New System.Windows.Forms.Label()
Me.label4 = New System.Windows.Forms.Label()
Me.label5 = New System.Windows.Forms.Label()
Me.label6 = New System.Windows.Forms.Label()
' Name Label
Me.label1.Location = New System.Drawing.Point(56, 32)
Me.label1.Size = New System.Drawing.Size(40, 23)
Me.label1.Text = "Name:"
' Age Label
Me.label2.Location = New System.Drawing.Point(40, 64)
Me.label2.Size = New System.Drawing.Size(56, 23)
Me.label2.Text = "Age (3-5)"
' Favorite Color Label
Me.label3.Location = New System.Drawing.Point(24, 96)
Me.label3.Size = New System.Drawing.Size(80, 24)
Me.label3.Text = "Favorite color"
' ErrorBlinkStyle.AlwaysBlink Label
Me.label4.Location = New System.Drawing.Point(264, 32)
Me.label4.Size = New System.Drawing.Size(160, 23)
Me.label4.Text = "ErrorBlinkStyle.AlwaysBlink"
' ErrorBlinkStyle.BlinkIfDifferentError Label
Me.label5.Location = New System.Drawing.Point(264, 64)
Me.label5.Size = New System.Drawing.Size(200, 23)
Me.label5.Text = "ErrorBlinkStyle.BlinkIfDifferentError"
' ErrorBlinkStyle.NeverBlink Label
Me.label6.Location = New System.Drawing.Point(264, 96)
Me.label6.Size = New System.Drawing.Size(200, 23)
Me.label6.Text = "ErrorBlinkStyle.NeverBlink"
' Name TextBox
Me.nameTextBox1.Location = New System.Drawing.Point(112, 32)
Me.nameTextBox1.Size = New System.Drawing.Size(120, 20)
Me.nameTextBox1.TabIndex = 0
' Age NumericUpDown
Me.ageUpDownPicker.Location = New System.Drawing.Point(112, 64)
Me.ageUpDownPicker.Maximum = New System.Decimal(New Integer() {150, 0, 0, 0})
Me.ageUpDownPicker.TabIndex = 4
' Favorite Color ComboBox
Me.favoriteColorComboBox.Items.AddRange(New Object() {"None", "Red", "Yellow", _
"Green", "Blue", "Purple"})
Me.favoriteColorComboBox.Location = New System.Drawing.Point(112, 96)
Me.favoriteColorComboBox.Size = New System.Drawing.Size(120, 21)
Me.favoriteColorComboBox.TabIndex = 5
' Set up how the form should be displayed and add the controls to the form.
Me.ClientSize = New System.Drawing.Size(464, 150)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.label6, Me.label5, Me.label4, _
Me.label3, Me.favoriteColorComboBox, Me.ageUpDownPicker, Me.label2, _
Me.label1, Me.nameTextBox1})
Me.Text = "Error Provider Example"
' Create and set the ErrorProvider for each data entry control.
nameErrorProvider = New System.Windows.Forms.ErrorProvider()
nameErrorProvider.SetIconAlignment(Me.nameTextBox1, ErrorIconAlignment.MiddleRight)
nameErrorProvider.SetIconPadding(Me.nameTextBox1, 2)
nameErrorProvider.BlinkRate = 1000
nameErrorProvider.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink
ageErrorProvider = New System.Windows.Forms.ErrorProvider()
ageErrorProvider.SetIconAlignment(Me.ageUpDownPicker, ErrorIconAlignment.MiddleRight)
ageErrorProvider.SetIconPadding(Me.ageUpDownPicker, 2)
ageErrorProvider.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.BlinkIfDifferentError
favoriteColorErrorProvider = New System.Windows.Forms.ErrorProvider()
favoriteColorErrorProvider.SetIconAlignment(Me.favoriteColorComboBox, ErrorIconAlignment.MiddleRight)
favoriteColorErrorProvider.SetIconPadding(Me.favoriteColorComboBox, 2)
favoriteColorErrorProvider.BlinkRate = 1000
favoriteColorErrorProvider.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.NeverBlink
End Sub
Private Sub nameTextBox1_Validated(sender As Object, e As System.EventArgs) Handles nameTextBox1.Validated
If IsNameValid() Then
' Clear the error, if any, in the error provider.
nameErrorProvider.SetError(Me.nameTextBox1, String.Empty)
Else
' Set the error if the name is not valid.
nameErrorProvider.SetError(Me.nameTextBox1, "Name is required.")
End If
End Sub
Private Sub ageUpDownPicker_Validated(sender As Object, e As System.EventArgs) Handles ageUpDownPicker.Validated
If IsAgeTooYoung() Then
' Set the error if the age is too young.
ageErrorProvider.SetError(Me.ageUpDownPicker, "Age not old enough")
ElseIf IsAgeTooOld() Then
' Set the error if the age is too old.
ageErrorProvider.SetError(Me.ageUpDownPicker, "Age is too old")
Else
' Clear the error, if any, in the error provider.
ageErrorProvider.SetError(Me.ageUpDownPicker, String.Empty)
End If
End Sub
Private Sub favoriteColorComboBox_Validated(sender As Object, e As System.EventArgs) Handles favoriteColorComboBox.Validated
If Not IsColorValid() Then
' Set the error if the favorite color is not valid.
favoriteColorErrorProvider.SetError(Me.favoriteColorComboBox, "Must select a color.")
Else
' Clear the error, if any, in the error provider.
favoriteColorErrorProvider.SetError(Me.favoriteColorComboBox, String.Empty)
End If
End Sub
' Functions to verify data.
Private Function IsNameValid() As Boolean
' Determine whether the text box contains a zero-length string.
Return nameTextBox1.Text.Length > 0
End Function
Private Function IsAgeTooYoung() As Boolean
' Determine whether the age value is less than three.
Return ageUpDownPicker.Value < 3
End Function
Private Function IsAgeTooOld() As Boolean
' Determine whether the age value is greater than five.
Return ageUpDownPicker.Value > 5
End Function
Private Function IsColorValid() As Boolean
' Determine whether the favorite color has a valid value.
If (favoriteColorComboBox.SelectedItem IsNot Nothing) Then
If Not(favoriteColorComboBox.SelectedItem.ToString().Equals("None")) Then
Return true
End If
End If
Return false
End Function
End Class
End Namespace 'ErrorProvider
Contoh kode berikut menunjukkan cara menggunakan ErrorProvider dengan DataSource dan DataMember untuk menunjukkan kesalahan data kepada pengguna.
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" ];
System::Collections::IEnumerator^ myEnum = (CustomersTable->Rows)->GetEnumerator();
while ( myEnum->MoveNext() )
{
DataRow^ row = safe_cast<DataRow^>(myEnum->Current);
if ( Convert::ToBoolean( row[ "PostalCodeIsNull" ] ) )
{
row->RowError = "The Customer details contain errors";
row->SetColumnError( "PostalCode", "Postal Code required" );
}
}
}
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");
}
}
}
Private Sub 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 = Me
errorProvider1.BlinkRate = 200
End Sub
'...
' Since the ErrorProvider control does not have a visible component,
' it does not need to be added to the form.
Private Sub buttonSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Checks for a bad post code.
Dim CustomersTable As DataTable
CustomersTable = dataSet1.Tables("Customers")
Dim row As DataRow
For Each row In CustomersTable.Rows
If Convert.ToBoolean(row("PostalCodeIsNull")) Then
row.RowError = "The Customer details contain errors"
row.SetColumnError("PostalCode", "Postal Code required")
End If
Next row
End Sub
Keterangan
ErrorProvider menyajikan mekanisme sederhana untuk menunjukkan kepada pengguna akhir bahwa kontrol pada formulir memiliki kesalahan yang terkait dengannya. Jika string deskripsi kesalahan ditentukan untuk kontrol, ikon muncul di samping kontrol. Ikon berkedip dengan cara yang ditentukan oleh BlinkStyle, pada laju yang ditentukan oleh BlinkRate. Saat mouse mengarahkan kursor ke ikon, TipsAlat muncul memperlihatkan string deskripsi kesalahan.
Biasanya, Anda menggunakan ErrorProvider dalam kaitannya dengan kontrol terikat data. Saat menggunakan ErrorProvider dengan kontrol terikat data, Anda harus menentukan ContainerControl, baik di konstruktor atau dengan mengatur ContainerControl properti .
Catatan
Komponen ErrorProvider tidak menyediakan dukungan bawaan untuk klien aksesibilitas. Untuk membuat aplikasi Anda dapat diakses saat menggunakan komponen ini, Anda harus menyediakan mekanisme umpan balik tambahan yang dapat diakses.
Konstruktor
ErrorProvider() |
Menginisialisasi instans ErrorProvider baru kelas dan menginisialisasi pengaturan default untuk BlinkRate, BlinkStyle, dan Icon. |
ErrorProvider(ContainerControl) |
Menginisialisasi instans baru kelas yang ErrorProvider dilampirkan ke kontainer. |
ErrorProvider(IContainer) |
Menginisialisasi instans baru kelas yang ErrorProviderIContainer dilampirkan ke implementasi. |
Properti
BlinkRate |
Mendapatkan atau mengatur laju di mana ikon kesalahan berkedip. |
BlinkStyle |
Mendapatkan atau menetapkan nilai yang menunjukkan kapan ikon kesalahan berkedip. |
CanRaiseEvents |
Mendapatkan nilai yang menunjukkan apakah komponen dapat menaikkan peristiwa. (Diperoleh dari Component) |
Container |
IContainer Mendapatkan yang berisi Component. (Diperoleh dari Component) |
ContainerControl |
Mendapatkan atau menetapkan nilai yang menunjukkan kontrol induk untuk ini ErrorProvider. |
DataMember |
Mendapatkan atau mengatur daftar dalam sumber data untuk dipantau. |
DataSource |
Mendapatkan atau mengatur sumber data yang dipantau ErrorProvider . |
DesignMode |
Mendapatkan nilai yang menunjukkan apakah Component saat ini dalam mode desain. (Diperoleh dari Component) |
Events |
Mendapatkan daftar penanganan aktivitas yang dilampirkan ke ini Component. (Diperoleh dari Component) |
HasErrors |
Mendapatkan nilai yang menunjukkan apakah ini ErrorProvider memiliki kesalahan untuk salah satu kontrol terkait. |
Icon |
Mendapatkan atau mengatur Icon yang ditampilkan di samping kontrol saat string deskripsi kesalahan telah diatur untuk kontrol. |
RightToLeft |
Mendapatkan atau menetapkan nilai yang menunjukkan apakah komponen digunakan dalam lokal yang mendukung font kanan-ke-kiri. |
Site | |
Tag |
Mendapatkan atau mengatur objek yang berisi data tentang komponen. |
Metode
BindToDataAndErrors(Object, String) |
Menyediakan metode untuk mengatur dan DataSourceDataMember pada durasi. |
CanExtend(Object) |
Mendapatkan nilai yang menunjukkan apakah kontrol dapat diperpanjang. |
Clear() |
Menghapus semua pengaturan yang terkait dengan komponen ini. |
CreateObjRef(Type) |
Membuat objek yang berisi semua informasi relevan yang diperlukan untuk menghasilkan proksi yang digunakan untuk berkomunikasi dengan objek jarak jauh. (Diperoleh dari MarshalByRefObject) |
Dispose() |
Merilis semua sumber daya yang Componentdigunakan oleh . (Diperoleh dari Component) |
Dispose(Boolean) |
Merilis sumber daya tidak terkelola yang digunakan oleh Component dan secara opsional merilis sumber daya terkelola. |
Equals(Object) |
Menentukan apakah objek yang ditentukan sama dengan objek saat ini. (Diperoleh dari Object) |
GetError(Control) |
Mengembalikan string deskripsi kesalahan saat ini untuk kontrol yang ditentukan. |
GetHashCode() |
Berfungsi sebagai fungsi hash default. (Diperoleh dari Object) |
GetIconAlignment(Control) |
Mendapatkan nilai yang menunjukkan di mana ikon kesalahan harus ditempatkan sehubungan dengan kontrol. |
GetIconPadding(Control) |
Mengembalikan jumlah ruang ekstra untuk diserahkan di samping ikon kesalahan. |
GetLifetimeService() |
Kedaluwarsa.
Mengambil objek layanan seumur hidup saat ini yang mengontrol kebijakan seumur hidup untuk instans ini. (Diperoleh dari MarshalByRefObject) |
GetService(Type) |
Mengembalikan objek yang mewakili layanan yang disediakan oleh Component atau oleh Container. (Diperoleh dari Component) |
GetType() |
Mendapatkan dari instans Type saat ini. (Diperoleh dari Object) |
InitializeLifetimeService() |
Kedaluwarsa.
Mendapatkan objek layanan seumur hidup untuk mengontrol kebijakan seumur hidup untuk instans ini. (Diperoleh dari MarshalByRefObject) |
MemberwiseClone() |
Membuat salinan dangkal dari saat ini Object. (Diperoleh dari Object) |
MemberwiseClone(Boolean) |
Membuat salinan dangkal objek saat ini MarshalByRefObject . (Diperoleh dari MarshalByRefObject) |
OnRightToLeftChanged(EventArgs) |
Memunculkan kejadian RightToLeftChanged. |
SetError(Control, String) |
Mengatur string deskripsi kesalahan untuk kontrol yang ditentukan. |
SetIconAlignment(Control, ErrorIconAlignment) |
Mengatur lokasi tempat ikon kesalahan harus ditempatkan dalam kaitannya dengan kontrol. |
SetIconPadding(Control, Int32) |
Mengatur jumlah ruang ekstra yang akan ditinggalkan antara kontrol yang ditentukan dan ikon kesalahan. |
ToString() |
Mengembalikan yang String berisi nama Component, jika ada. Metode ini tidak boleh ditimpa. (Diperoleh dari Component) |
UpdateBinding() |
Menyediakan metode untuk memperbarui pengikatan DataSource, DataMember, dan teks kesalahan. |
Acara
Disposed |
Terjadi ketika komponen dibuang oleh panggilan ke Dispose() metode . (Diperoleh dari Component) |
RightToLeftChanged |
Terjadi saat RightToLeft properti mengubah nilai. |
Implementasi Antarmuka Eksplisit
ISupportInitialize.BeginInit() |
Menandakan objek yang memulai inisialisasi. |
ISupportInitialize.EndInit() |
Memberi sinyal objek bahwa inisialisasi selesai. |