Control.Leave Control.Leave Control.Leave Control.Leave Event

Definición

Se produce cuando el foco de entrada deja el control.Occurs when the input focus leaves the control.

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

Ejemplos

El siguiente ejemplo de código utiliza el Leave eventos para restablecer un control a su estado anterior.The following code example uses the Leave event to reset a control to its former state.

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 'textBox1_Enter
   
   
    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 'textBox1_Leave
End Class 'Form1 

Comentarios

Al cambiar el foco mediante el teclado (ficha, MAYÚS+TAB etc.), mediante una llamada a la Select o SelectNextControl métodos, o estableciendo la ContainerControl.ActiveControl propiedad al formulario actual, se producen eventos de foco en el orden siguiente:When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

Al cambiar el foco mediante el mouse o mediante una llamada a la Focus método, se producen eventos de foco en el orden siguiente:When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

Si el CausesValidation propiedad está establecida en false, Validating y Validated se suprimen eventos.If the CausesValidation property is set to false, the Validating and Validated events are suppressed.

Nota

El Enter y Leave eventos se han suprimido por el Form clase.The Enter and Leave events are suppressed by the Form class. Los eventos equivalentes en el Form clase son el Activated y Deactivate eventos.The equivalent events in the Form class are the Activated and Deactivate events. El Enter y Leave eventos son jerárquicos y se realizará en cascada arriba y abajo de la cadena primaria hasta que se alcance el control adecuado.The Enter and Leave events are hierarchical and will cascade up and down the parent chain until the appropriate control is reached. Por ejemplo, suponga que tiene un Form con dos GroupBox controles y cada GroupBox control tiene uno TextBox control.For example, assume you have a Form with two GroupBox controls, and each GroupBox control has one TextBox control. Cuando se mueve el símbolo de intercalación de una TextBox a otro, el Leave evento se desencadena para el TextBox y GroupBoxy el Enter evento se desencadena para los demás GroupBox y TextBox.When the caret is moved from one TextBox to the other, the Leave event is raised for the TextBox and GroupBox, and the Enter event is raised for the other GroupBox and TextBox.

Precaución

No intente establecer el foco desde el Enter, GotFocus, Leave, LostFocus, Validating, o Validated controladores de eventos.Do not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers. Si lo hace, puede provocar la aplicación o el sistema operativo deja de responder.Doing so can cause your application or the operating system to stop responding. Para obtener más información, consulte el WM_KILLFOCUS tema.For more information, see the WM_KILLFOCUS topic.

Para obtener más información sobre el manejo de eventos, consulte controlar y provocar eventos.For more information about handling events, see Handling and Raising Events.

Se aplica a

Consulte también: