Control.Enter Evento

Definición

Se produce cuando se entra en el control.Occurs when the control is entered.

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

Ejemplos

En el ejemplo de código siguiente se usa el evento Enter para cambiar los colores de primer plano y de fondo de un TextBox en determinadas condiciones.The following code example uses the Enter event to change the foreground and background colors of a TextBox under particular conditions.

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 métodos Select o SelectNextControl, o estableciendo la propiedad ContainerControl.ActiveControl 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 método Focus, 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 propiedad CausesValidation está establecida en false, se suprimen los eventos Validating y Validated.If the CausesValidation property is set to false, the Validating and Validated events are suppressed.

Nota

La clase Form suprime los eventos Enter y Leave.The Enter and Leave events are suppressed by the Form class. Los eventos equivalentes en la clase Form son los eventos Activated y Deactivate.The equivalent events in the Form class are the Activated and Deactivate events. Los eventos Enter y Leave 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 controles GroupBox, y cada control GroupBox tiene un control TextBox.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 mueve de un TextBox al otro, se genera el evento Leave para los TextBox y GroupBox, y el evento Enter se genera para los otros 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 los controladores de eventos Enter, GotFocus, Leave, LostFocus, Validatingo 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 en la sección "referencia de entrada de teclado" y la sección "interbloqueos de mensajes" del tema acerca de mensajes y colas de mensajes.For more information, see the WM_KILLFOCUS topic in the "Keyboard Input Reference" section and the "Message Deadlocks" section of the About Messages and Message Queues 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: