Freigeben über


Control.Leave-Ereignis

Tritt ein, wenn der Eingabefokus das Steuerelement verlässt.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Event Leave As EventHandler
'Usage
Dim instance As Control
Dim handler As EventHandler

AddHandler instance.Leave, handler
public event EventHandler Leave
public:
event EventHandler^ Leave {
    void add (EventHandler^ value);
    void remove (EventHandler^ value);
}
/** @event */
public void add_Leave (EventHandler value)

/** @event */
public void remove_Leave (EventHandler value)
JScript unterstützt die Verwendung von Ereignissen, aber nicht die Deklaration von neuen Ereignissen.

Hinweise

Wenn Sie den Fokus mithilfe der Tastatur (TAB, UMSCHALT+TAB usw.), durch Aufrufen der Select-Methode oder der SelectNextControl-Methode oder durch Festlegen der ContainerControl.ActiveControl-Eigenschaft auf das aktuelle Formular ändern, treten die Fokusereignisse in der folgenden Reihenfolge ein:

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

Wenn Sie den Fokus mit der Maus oder durch Aufrufen der Focus-Methode ändern, treten die Fokusereignisse in der folgenden Reihenfolge ein:

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

Wenn die CausesValidation-Eigenschaft auf false festgelegt ist, wird das Validating-Ereignis und das Validated-Ereignis unterdrückt.

Hinweis

Das Enter-Ereignis und das Leave-Ereignis werden von der Form-Klasse unterdrückt. Die entsprechenden Ereignisse in der Form-Klasse sind das Activated-Ereignis und das Deactivate-Ereignis. Das Enter-Ereignis und das Leave-Ereignis sind hierarchische Ereignisse. Sie durchlaufen die Kette übergeordneter Elemente, bis das richtige Steuerelement erreicht wird. Angenommen, es liegt ein Form mit zwei GroupBox-Steuerelementen vor, wobei jedes GroupBox-Steuerelement ein TextBox-Steuerelement aufweist. Wenn das Caretzeichen von einer TextBox zur anderen verschoben wird, wird das Leave-Ereignis für die erste TextBox und GroupBox ausgelöst, und das Enter-Ereignis wird für die zweite GroupBox und TextBox ausgelöst.

Weitere Informationen zum Behandeln von Ereignissen finden Sie unter Behandeln von Ereignissen.

Beispiel

Im folgenden Codebeispiel wird mit dem Leave-Ereignis ein Steuerelement auf seinen früheren Zustand zurückgesetzt.

    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 'textBox1_Enter
   
   
    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 'textBox1_Leave
End Class 'Form1 
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);
    }
}

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:
   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 (!(textBox1.get_Text().Equals(""))) {
        textBox1.set_ForeColor(Color.get_Red());
        textBox1.set_BackColor(Color.get_Black());

        // Move the selection pointer to the end of the text of the 
        // control.
        textBox1.Select(textBox1.get_Text().get_Length(), 0);
    }
} //textBox1_Enter

private void textBox1_Leave(Object sender, System.EventArgs e)
{
    // Reset the colors and selection of the TextBox after focus is lost.
    textBox1.set_ForeColor(Color.get_Black());
    textBox1.set_BackColor(Color.get_White());
    textBox1.Select(0, 0);
} //textBox1_Leave

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

Control-Klasse
Control-Member
System.Windows.Forms-Namespace
OnLeave
Control.Enter-Ereignis