Control.Enter Evento

Definição

Ocorre quando o controle é inserido.Occurs when the control is entered.

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

Exemplos

O exemplo de código a seguir usa o evento Enter para alterar as cores de primeiro e segundo plano de um TextBox sob condições específicas.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

Comentários

Quando você altera o foco usando o teclado (TAB, SHIFT+TAB etc.), chamando os métodos Select ou SelectNextControl, ou definindo a propriedade ContainerControl.ActiveControl como o formulário atual, os eventos de foco ocorrem na seguinte ordem: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

Quando você altera o foco usando o mouse ou chamando o método Focus, os eventos de foco ocorrem na seguinte ordem: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

Se a propriedade CausesValidation for definida como false, os eventos Validating e Validated serão suprimidos.If the CausesValidation property is set to false, the Validating and Validated events are suppressed.

Observação

Os eventos Enter e Leave são suprimidos pela classe Form.The Enter and Leave events are suppressed by the Form class. Os eventos equivalentes na classe Form são os eventos Activated e Deactivate.The equivalent events in the Form class are the Activated and Deactivate events. Os eventos Enter e Leave são hierárquicos e serão colocados em cascata na cadeia pai até que o controle apropriado seja atingido.The Enter and Leave events are hierarchical and will cascade up and down the parent chain until the appropriate control is reached. Por exemplo, suponha que você tenha um Form com dois controles GroupBox e que cada controle GroupBox tenha um controle TextBox.For example, assume you have a Form with two GroupBox controls, and each GroupBox control has one TextBox control. Quando o cursor é movido de um TextBox para outro, o evento Leave é gerado para o TextBox e GroupBox, e o evento Enter é gerado para os outros GroupBox e 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.

Cuidado

Não tente definir o foco nos manipuladores de eventos Enter, GotFocus, Leave, LostFocus, Validating ou Validated.Do not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers. Isso pode fazer com que o aplicativo ou o sistema operacional pare de responder.Doing so can cause your application or the operating system to stop responding. Para obter mais informações, consulte o tópico WM_KILLFOCUS na seção "referência de entrada de teclado" e a seção "deadlocks de mensagem" do tópico sobre mensagens e filas de mensagens.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 obter mais informações sobre como manipular eventos, consulte manipulando e gerando eventos.For more information about handling events, see Handling and Raising Events.

Aplica-se a

Veja também