Partager via


Control.Leave Événement

Définition

Se produit lorsque le focus d’entrée quitte le contrôle.

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

Type d'événement

Exemples

L’exemple de code suivant utilise l’événement Leave pour réinitialiser un contrôle à son ancien état.

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

Remarques

Lorsque vous modifiez le focus à l’aide du clavier (TAB, Maj+Tab, et ainsi de suite), en appelant ou SelectSelectNextControl en définissant la ContainerControl.ActiveControl propriété sur le formulaire actif, les événements de focus se produisent dans l’ordre suivant :

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

Lorsque vous modifiez le focus à l’aide de la souris ou en appelant la Focus méthode, les événements de focus se produisent dans l’ordre suivant :

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

Si la CausesValidation propriété est définie falsesur , les Validating événements et Validated les événements sont supprimés.

Note

Les Enter événements et Leave les événements sont supprimés par la Form classe. Les événements équivalents dans la Form classe sont les Activated événements et Deactivate les événements. Les Enter événements et Leave les événements sont hiérarchiques et descendront en cascade jusqu’à ce que le contrôle approprié soit atteint. Par exemple, supposons que vous disposez de Form deux GroupBox contrôles et que chaque GroupBox contrôle a un TextBox contrôle. Lorsque le caret est déplacé de l’un TextBox à l’autre, l’événement Leave est déclenché pour l’et TextBoxGroupBox, et l’événement Enter est déclenché pour l’autre GroupBox et TextBox.

Avertissement

N’essayez pas de définir le focus à partir des gestionnaires d’événements, Validated et ValidatingLostFocusLeaveGotFocusnon à partir de l’intérieur des Entergestionnaires d’événements. Cela peut entraîner l’arrêt de la réponse de votre application ou du système d’exploitation. Pour plus d’informations, consultez la rubrique WM_KILLFOCUS .

Pour plus d'informations sur la gestion des événements, voir gestion et déclenchement d’événements.

S’applique à

Voir aussi