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

En el ejemplo de código siguiente Leave se utiliza el evento para restablecer el estado anterior de un control.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
   
   
    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 'Form1 

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: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 llamando al Focus método, los eventos de foco se producen 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 la CausesValidation propiedad se establece en false, se Validating suprimen los eventos y Validated .If the CausesValidation property is set to false, the Validating and Validated events are suppressed.

Nota

La Enter claseForm suprime los eventos y Leave .The Enter and Leave events are suppressed by the Form class. Los eventos equivalentes de Form la clase son Activated los Deactivate eventos y.The equivalent events in the Form class are the Activated and Deactivate events. Los Enter eventos Leave y son jerárquicos y se colocarán en cascada hacia arriba y hacia abajo en 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 un TextBox control.For example, assume you have a Form with two GroupBox controls, and each GroupBox control has one TextBox control. Cuando el símbolo de intercalación se TextBox mueve de uno a otro Leave , el TextBox evento se genera para GroupBoxy, y Enter el evento se genera para el GroupBox otro TextBoxy.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 Enterlos controladores de eventos, Leave GotFocus, LostFocus, Validating, o Validated .Do not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers. Esto puede hacer que la aplicación o el sistema operativo dejen de responder.Doing so can cause your application or the operating system to stop responding. Para obtener más información, vea el tema WM_KILLFOCUS .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: