次の方法で共有


ErrorProvider.SetError メソッド

指定したコントロールに対し、エラーを説明する文字列を設定します。

Public Sub SetError( _
   ByVal control As Control, _   ByVal value As String _)
[C#]
public void SetError(Controlcontrol,stringvalue);
[C++]
public: void SetError(Control* control,String* value);
[JScript]
public function SetError(
   control : Control,value : String);

パラメータ

  • control
    エラーを説明する文字列を設定する対象となるコントロール。
  • value
    エラーを説明する文字列。

解説

文字列の長さが 0 以上の場合は、エラー アイコンが表示され、エラー アイコンのツール ヒントの部分はエラーを説明するテキストになります。文字列の長さが 0 の場合、エラー アイコンは非表示になります。

使用例

[Visual Basic, C#, C++] ErrorProvider クラスを使用して、データ入力エラーをユーザーに通知する方法を次の例に示します。 TextBox コントロール、 NumericUpDown コントロール、および ComboBox コントロールが配置された Form を作成する例を次に示します。内容の検証は各コントロールで実行し、各コントロールの ErrorProvider も作成します。 BlinkRate プロパティと BlinkStyle プロパティ、および SetIconAlignment メソッドと SetIconPadding メソッドを使用してエラー アイコンのオプションを設定する例を次に示します。 SetError メソッドは、コントロールの内容に応じて、コントロールの Validated イベント時に、適切なエラー テキスト付きまたはエラー テキストなしで呼び出されます。

 
Imports System
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 'Main

        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.ageUpDownPicker, ErrorIconAlignment.MiddleRight)
            favoriteColorErrorProvider.SetIconPadding(Me.ageUpDownPicker, 2)
            favoriteColorErrorProvider.BlinkRate = 1000
            favoriteColorErrorProvider.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.NeverBlink
        End Sub 'New
 
        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, "")
            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, "")
            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, "")
            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 Not (favoriteColorComboBox.SelectedItem Is Nothing) Then
                    If Not(favoriteColorComboBox.SelectedItem.ToString().Equals("None")) Then
                        Return true 
                    End If
            End If
            Return false
        End Function 

    End Class 'Form1
End Namespace 'ErrorProvider 

[C#] 
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.ageUpDownPicker, ErrorIconAlignment.MiddleRight);
            favoriteColorErrorProvider.SetIconPadding (this.ageUpDownPicker, 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")));
        }
    }
}

[C++] 
#using <mscorlib.dll>
#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 __gc 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 = 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 =  System::Drawing::Point(56, 32);
      this->label1->Size =  System::Drawing::Size(40, 23);
      this->label1->Text = S"Name:";

      // Age Label
      this->label2->Location =  System::Drawing::Point(40, 64);
      this->label2->Size =  System::Drawing::Size(56, 23);
      this->label2->Text = S"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 = S"Favorite color";

      // ErrorBlinkStyle::AlwaysBlink Label
      this->label4->Location =  System::Drawing::Point(264, 32);
      this->label4->Size =  System::Drawing::Size(160, 23);
      this->label4->Text = S"ErrorBlinkStyle::AlwaysBlink";

      // ErrorBlinkStyle::BlinkIfDifferentError Label
      this->label5->Location =  System::Drawing::Point(264, 64);
      this->label5->Size =  System::Drawing::Size(200, 23);
      this->label5->Text = S"ErrorBlinkStyle::BlinkIfDifferentError";

      // ErrorBlinkStyle::NeverBlink Label
      this->label6->Location =  System::Drawing::Point(264, 96);
      this->label6->Size =  System::Drawing::Size(200, 23);
      this->label6->Text = S"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 += new System::EventHandler(this, &Form1::nameTextBox1_Validated);

      // Age NumericUpDown
      this->ageUpDownPicker->Location =  System::Drawing::Point(112, 64);

      int temp0 __gc [] = {150, 0, 0, 0};

      this->ageUpDownPicker->Maximum =  System::Decimal(temp0);
      this->ageUpDownPicker->TabIndex = 4;
      this->ageUpDownPicker->Validated += new System::EventHandler(this, &Form1::ageUpDownPicker_Validated);

      // Favorite Color ComboBox

      Object* temp1 [] = {S"None", S"Red", S"Yellow", S"Green", S"Blue", S"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 += new 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);

      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 = S"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->ageUpDownPicker, ErrorIconAlignment::MiddleRight);
      favoriteColorErrorProvider->SetIconPadding (this->ageUpDownPicker, 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, S"");
      } else {
         // Set the error if the name is not valid.
         nameErrorProvider->SetError(this->nameTextBox1, S"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, S"Age not old enough");
      } else if (IsAgeTooOld()) {
         // Set the error if the age is too old.
         ageErrorProvider->SetError(this->ageUpDownPicker, S"Age is too old");
      } else {
         // Clear the error, if any, in the error provider.
         ageErrorProvider->SetError(this->ageUpDownPicker, S"");
      }
   }

   void favoriteColorComboBox_Validated(Object* /*sender*/, System::EventArgs* /*e*/) {
      if (!IsColorValid()) {
         // Set the error if the favorite color is not valid.
         favoriteColorErrorProvider->SetError(this->favoriteColorComboBox, S"Must select a color.");
      } else {
         // Clear the error, if any, in the error provider.
         favoriteColorErrorProvider->SetError(this->favoriteColorComboBox, S"");
      }
   }

   // 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(S"None")));
   }
};

[STAThread]
int main() {
   Application::Run(new Form1());
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

ErrorProvider クラス | ErrorProvider メンバ | System.Windows.Forms 名前空間 | GetError