次の方法で共有


Control.Validating イベント

コントロールが検証を行っているときに発生します。

Public Event Validating As CancelEventHandler
[C#]
public event CancelEventHandler Validating;
[C++]
public: __event CancelEventHandler* Validating;

[JScript] JScript では、このクラスで定義されているイベントを処理できます。ただし、独自に定義することはできません。

イベント データ

イベント ハンドラが、このイベントに関連するデータを含む、CancelEventArgs 型の引数を受け取りました。次の CancelEventArgs プロパティには、このイベントの固有の情報が記載されます。

プロパティ 説明
Cancel イベントをキャンセルするかどうかを示す値を取得または設定します。

解説

キーボード (Tab、Shift+Tab など) を使用するか、 Select メソッドまたは SelectNextControl メソッドを呼び出すか、 ContainerControl.ActiveControl プロパティを現在のフォームに設定してフォーカスを変更するとき、次の順序でフォーカス イベントが発生します。

  1. Enter
  2. GotFocus
  3. Leave
  4. Validating
  5. Validated
  6. LostFocus

マウスを使用するか Focus メソッドを呼び出してフォーカスを変更するとき、フォーカス イベントは次の順序で発生します。

  1. Enter
  2. GotFocus
  3. LostFocus
  4. Leave
  5. Validating
  6. Validated

CausesValidation プロパティが false に設定されている場合、 Validating イベントおよび Validated イベントは発生しません。

Validating イベント デリゲートで CancelEventArgs オブジェクトの Cancel プロパティが true に設定されると、通常は Validating イベントの後に発生するすべてのイベントが発生しなくなります。

イベント処理の詳細については、「 イベントの利用 」を参照してください。

使用例

[Visual Basic, C#, C++] 派生クラス TextBox を使用して、ユーザーが入力した電子メール アドレスを検証する例を次に示します。電子メール アドレスが標準の書式 ("@" および "." を含む) ではない場合、検証は失敗し、 ErrorProvider アイコンが表示され、イベントはキャンセルされます。この例は、 TextBox および ErrorProvider コントロールがフォーム上で作成されていることを前提にしています。

 
Private Function ValidEmailAddress(ByVal emailAddress As String, ByRef errorMessage As String) As Boolean
   ' Confirm there is text in the control.
   If textBox1.Text.Length = 0 Then
      errorMessage = "E-mail address is required."
      Return False

   End If

   ' Confirm that there is an "@" and a "." in the e-mail address, and in the correct order.
   If emailAddress.IndexOf("@") > -1 Then
      If (emailAddress.IndexOf(".", emailAddress.IndexOf("@")) > emailAddress.IndexOf("@")) Then
         errorMessage = ""
         Return True
      End If
   End If

   errorMessage = "E-mail address must be valid e-mail address format." + ControlChars.Cr + _
     "For example 'someone@example.com' "
   Return False
End Function

Private Sub textBox1_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles textBox1.Validating

   Dim errorMsg As String
   If Not ValidEmailAddress(textBox1.Text, errorMsg) Then
      ' Cancel the event and select the text to be corrected by the user.
      e.Cancel = True
      textBox1.Select(0, textBox1.Text.Length)

      ' Set the ErrorProvider error with the text to display. 
      Me.errorProvider1.SetError(textBox1, errorMsg)
   End If
End Sub


Private Sub textBox1_Validated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles textBox1.Validated
   ' If all conditions have been met, clear the error provider of errors.
   errorProvider1.SetError(textBox1, "")
End Sub

[C#] 
private void textBox1_Validating(object sender, 
                 System.ComponentModel.CancelEventArgs e)
{
   string errorMsg;
   if(!ValidEmailAddress(textBox1.Text, out errorMsg))
   {
      // Cancel the event and select the text to be corrected by the user.
      e.Cancel = true;
      textBox1.Select(0, textBox1.Text.Length);

      // Set the ErrorProvider error with the text to display. 
      this.errorProvider1.SetError(textBox1, errorMsg);
   }
}

private void textBox1_Validated(object sender, System.EventArgs e)
{
   // If all conditions have been met, clear the ErrorProvider of errors.
   errorProvider1.SetError(textBox1, "");
}
public bool ValidEmailAddress(string emailAddress, out string errorMessage)
{
   // Confirm that the e-mail address string is not empty.
   if(emailAddress.Length == 0)
   {
      errorMessage = "e-mail address is required.";
         return false;
   }

   // Confirm that there is an "@" and a "." in the e-mail address, and in the correct order.
   if(emailAddress.IndexOf("@") > -1)
   {
      if(emailAddress.IndexOf(".", emailAddress.IndexOf("@") ) > emailAddress.IndexOf("@") )
      {
         errorMessage = "";
         return true;
      }
   }
   
   errorMessage = "e-mail address must be valid e-mail address format.\n" +
      "For example 'someone@example.com' ";
      return false;
}

[C++] 
private:
    void textBox1_Validating(Object* /*sender*/,
        System::ComponentModel::CancelEventArgs* e) {
            String* errorMsg;
            if (!ValidEmailAddress(textBox1->Text, &errorMsg)) {
                // Cancel the event and select the text to be corrected by the user.
                e->Cancel = true;
                textBox1->Select(0, textBox1->Text->Length);

                // Set the ErrorProvider error with the text to display.
                this->errorProvider1->SetError(textBox1, errorMsg);
            }
    }

    void textBox1_Validated(Object* /*sender*/, System::EventArgs* /*e*/) {
        // If all conditions have been met, clear the ErrorProvider of errors.
        errorProvider1->SetError(textBox1, S"");
    }

public:
    bool ValidEmailAddress(String* emailAddress, [Out] String** errorMessage) {
        // Confirm that the e-mail address String* is not empty.
        if (emailAddress->Length == 0) {
            *errorMessage = S"e-mail address is required.";
            return false;
        }

        // Confirm that there is an S"@" and a S"." in the e-mail address, and in the correct order.
        if (emailAddress->IndexOf(S"@") > -1) {
            if (emailAddress->IndexOf(S".", emailAddress->IndexOf(S"@")) > emailAddress->IndexOf(S"@")) {
                *errorMessage = S"";
                return true;
            }
        }

        *errorMessage = S"e-mail address must be valid e-mail address format.\n For example 'someone@example.com' ";
        return false;
    }

[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 ファミリ

参照

Control クラス | Control メンバ | System.Windows.Forms 名前空間 | OnValidating | CausesValidation | Validated