Control.Leave Evento

Definição

Ocorre quando o foco de entrada deixa o controle.

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

Exemplos

O exemplo de código a seguir usa o Leave evento para redefinir um controle para seu 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

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:

  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:

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

Se a CausesValidation propriedade estiver definida como false, os Validating eventos e Validated serão suprimidos.

Observação

Os Enter eventos e Leave são suprimidos pela Form classe . Os eventos equivalentes na Form classe são os Activated eventos e Deactivate . Os Enter eventos e Leave são hierárquicos e serão em cascata para cima e para baixo na cadeia pai até que o controle apropriado seja atingido. Por exemplo, suponha que você tenha um Form com dois GroupBox controles e cada GroupBox controle tenha um TextBox controle. Quando o cursor é movido de um TextBox para o outro, o Leave evento é gerado para e TextBoxGroupBoxe o Enter evento é gerado para o outro GroupBox e TextBox.

Cuidado

Não tente definir o foco de dentro dos Entermanipuladores de eventos , LeaveGotFocus, LostFocus, , Validatingou Validated . Isso pode fazer com que o aplicativo ou o sistema operacional pare de responder. Para obter mais informações, consulte o tópico WM_KILLFOCUS .

Para obter mais informações sobre como lidar com eventos, consulte Manipulando e gerando eventos.

Aplica-se a

Confira também