Freigeben über


Control.Validating-Ereignis

Tritt ein, während das Steuerelement eine Überprüfung durchführt.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Event Validating As CancelEventHandler
'Usage
Dim instance As Control
Dim handler As CancelEventHandler

AddHandler instance.Validating, handler
public event CancelEventHandler Validating
public:
event CancelEventHandler^ Validating {
    void add (CancelEventHandler^ value);
    void remove (CancelEventHandler^ value);
}
/** @event */
public void add_Validating (CancelEventHandler value)

/** @event */
public void remove_Validating (CancelEventHandler value)
JScript unterstützt die Verwendung von Ereignissen, aber nicht die Deklaration von neuen Ereignissen.

Hinweise

Wenn Sie den Fokus mithilfe der Tastatur (TAB, UMSCHALT+TAB usw.), durch Aufrufen der Select-Methode oder der SelectNextControl-Methode oder durch Festlegen der ContainerControl.ActiveControl-Eigenschaft auf das aktuelle Formular ändern, treten die Fokusereignisse in der folgenden Reihenfolge ein:

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

Wenn Sie den Fokus mit der Maus oder durch Aufrufen der Focus-Methode ändern, treten die Fokusereignisse in der folgenden Reihenfolge ein:

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

Wenn die CausesValidation-Eigenschaft auf false festgelegt ist, werden das Validating-Ereignis und das Validated-Ereignis unterdrückt.

Wenn die Cancel-Eigenschaft von CancelEventArgs im Validating-Ereignisdelegaten auf true festgelegt wird, werden alle normalerweise nach dem Validating-Ereignis eintretenden Ereignisse unterdrückt.

Weitere Informationen zum Behandeln von Ereignissen finden Sie unter Behandeln von Ereignissen.

Beispiel

Im folgenden Codebeispiel wird die abgeleitete Klasse TextBox verwendet und eine vom Benutzer eingegebene E-Mail-Adresse überprüft. Wenn die E-Mail-Adresse nicht das Standardformat hat (d. h. "@" und "." enthält), schlägt die Validierung fehl, ein ErrorProvider-Symbol wird angezeigt, und das Ereignis wird abgebrochen. Für dieses Beispiel müssen zuvor in einem Formular ein TextBox-Steuerelement und ein ErrorProvider-Steuerelement erstellt werden.

   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
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;
}
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, "" );
   }

public:
   bool ValidEmailAddress( String^ emailAddress, [Out]interior_ptr<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;
   }
private void textBox1_Validating(Object sender, 
    System.ComponentModel.CancelEventArgs e)
{
    String errorMsg = "";

    if (!(ValidEmailAddress(textBox1.get_Text(), errorMsg))) {
        // Cancel the event and select the text to be corrected by the user.
        e.set_Cancel(true);
        textBox1.Select(0, textBox1.get_Text().get_Length());

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

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

public boolean ValidEmailAddress(String emailAddress, 
    /** @ref 
     */ String errorMessage)
{
    // Confirm that the e-mail address string is not empty.
    if (emailAddress.get_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;
} //ValidEmailAddress

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Control-Klasse
Control-Member
System.Windows.Forms-Namespace
OnValidating
Control.CausesValidation-Eigenschaft
Control.Validated-Ereignis