Control.Validated 事件

在控件完成验证时发生。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Event Validated As EventHandler
用法
Dim instance As Control
Dim handler As EventHandler

AddHandler instance.Validated, handler
public event EventHandler Validated
public:
event EventHandler^ Validated {
    void add (EventHandler^ value);
    void remove (EventHandler^ value);
}
/** @event */
public void add_Validated (EventHandler value)

/** @event */
public void remove_Validated (EventHandler value)
JScript 支持使用事件,但不支持进行新的声明。

备注

当通过使用键盘(Tab、Shift+Tab 等)、通过调用 SelectSelectNextControl 方法或者通过将 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,则将取消 ValidatingValidated 事件。

如果在 Validating 事件委托中,CancelEventArgsCancel 属性设置为 true,则正常情况下将在 Validating 事件之后发生的所有事件均被取消。

有关处理事件的更多信息,请参见 使用事件

示例

下面的代码示例使用派生类 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
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

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Control 类
Control 成员
System.Windows.Forms 命名空间
OnValidated
Control.CausesValidation 属性
Validating