Control.Leave Händelse

Definition

Inträffar när indatafokus lämnar kontrollen.

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

Händelsetyp

Exempel

I följande kodexempel används Leave händelsen för att återställa en kontroll till dess tidigare tillstånd.

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

Kommentarer

När du ändrar fokus med hjälp av tangentbordet (TAB, SKIFT+TAB och så vidare), genom att anropa Select metoderna eller SelectNextControl eller genom att ange ContainerControl.ActiveControl egenskapen till det aktuella formuläret, sker fokushändelser i följande ordning:

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

När du ändrar fokus med hjälp av musen eller genom att anropa Focus metoden sker fokushändelser i följande ordning:

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

Om egenskapen CausesValidation är inställd på falseutelämnas Validating händelserna och Validated .

Note

Händelserna Enter och Leave undertrycks av Form klassen. Motsvarande händelser i Form klassen är Activated händelserna och Deactivate . Händelserna Enter och Leave är hierarkiska och kommer att kaskaderas upp och ned i den överordnade kedjan tills rätt kontroll har nåtts. Anta till exempel att du har en Form med två GroupBox kontroller och att varje GroupBox kontroll har en TextBox kontroll. När caret flyttas från en TextBox till en annan Leave aktiveras händelsen för TextBox och GroupBox, och Enter händelsen aktiveras för den andra GroupBox och TextBox.

Caution

Försök inte att ange fokus inifrån Enterhändelsehanterarna , GotFocus, LeaveLostFocus, , Validatingeller Validated . Om du gör det kan programmet eller operativsystemet sluta svara. Mer information finns i avsnittet WM_KILLFOCUS .

Mer information om hur du hanterar händelser finns i Hantera och höja händelser.

Gäller för

Se även