Control.Leave Evento

Definición

Se produce cuando el foco de entrada deja el control.

public:
 event EventHandler ^ Leave;
public event EventHandler Leave;
public event EventHandler? Leave;
member this.Leave : EventHandler 
Public Custom Event Leave As EventHandler 

Tipo de evento

Ejemplos

En el ejemplo de código siguiente se usa el Leave evento para restablecer un control a su estado anterior.

private:
   void textBox1_Enter( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // If the TextBox contains text, change its foreground and background colors.
      if ( textBox1->Text != String::Empty )
      {
         textBox1->ForeColor = Color::Red;
         textBox1->BackColor = Color::Black;

         // Move the selection pointer to the end of the text of the control.
         textBox1->Select(textBox1->Text->Length,0);
      }
   }

   void textBox1_Leave( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Reset the colors and selection of the TextBox after focus is lost.
      textBox1->ForeColor = Color::Black;
      textBox1->BackColor = Color::White;
      textBox1->Select(0,0);
   }
private void textBox1_Enter(object sender, System.EventArgs e)
{
    // If the TextBox contains text, change its foreground and background colors.
    if (!string.IsNullOrEmpty(textBox1.Text))
    {
        textBox1.ForeColor = Color.Red;
        textBox1.BackColor = Color.Black;
        // Move the selection pointer to the end of the text of the control.
        textBox1.Select(textBox1.Text.Length, 0);
    }
}

private void textBox1_Leave(object sender, System.EventArgs e)
{
    // Reset the colors and selection of the TextBox after focus is lost.
    textBox1.ForeColor = Color.Black;
    textBox1.BackColor = Color.White;
    textBox1.Select(0,0);
}
    Private Sub textBox1_Enter(sender As Object, e As System.EventArgs) Handles textBox1.Enter
        ' If the TextBox contains text, change its foreground and background colors.
        If textBox1.Text <> [String].Empty Then
            textBox1.ForeColor = Color.Red
            textBox1.BackColor = Color.Black
            ' Move the selection pointer to the end of the text of the control.
            textBox1.Select(textBox1.Text.Length, 0)
        End If
    End Sub
   
   
    Private Sub textBox1_Leave(sender As Object, e As System.EventArgs) Handles textBox1.Leave
        ' Reset the colors and selection of the TextBox after focus is lost.
        textBox1.ForeColor = Color.Black
        textBox1.BackColor = Color.White
        textBox1.Select(0, 0)
    End Sub
End Class

Comentarios

Al cambiar el foco mediante el teclado (TAB, MAYÚS+TAB, etc.), llamando a los Select métodos o SelectNextControl , o estableciendo la ContainerControl.ActiveControl propiedad en el formulario actual, los eventos de foco se producen en el orden siguiente:

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

Al cambiar el foco mediante el mouse o llamando al Focus método , los eventos de foco se producen en el orden siguiente:

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

Si la CausesValidation propiedad se establece falseen , se suprimen los Validating eventos y Validated .

Nota

La clase suprime Form los Enter eventos y Leave . Los eventos equivalentes de la Form clase son los Activated eventos y Deactivate . Los Enter eventos y Leave son jerárquicos y subirán y bajarán la cadena primaria hasta que se alcance el control adecuado. Por ejemplo, supongamos que tiene un Form con dos GroupBox controles y que cada GroupBox control tiene un TextBox control. Cuando el símbolo de intercalación se mueve de uno TextBox a otro, el Leave evento se genera para TextBox y GroupBox, y el Enter evento se genera para el otro GroupBox y TextBox.

Precaución

No intente establecer el foco desde los Entercontroladores de eventos , , LostFocusGotFocusLeave, , Validatingo Validated . Si lo hace, la aplicación o el sistema operativo dejarán de responder. Para obtener más información, consulte el tema WM_KILLFOCUS .

Para obtener más información sobre el manejo de eventos, consulte controlar y provocar eventos.

Se aplica a

Consulte también