共用方式為


Control.Leave 事件

定義

當輸入焦點離開控制器時會發生。

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

事件類型

範例

以下程式碼範例利用事件 Leave 將控制項重置至先前狀態。

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

備註

當你用鍵盤(TAB、SHIFT+TAB 等)、呼叫 Select or SelectNextControl 方法,或將屬性設 ContainerControl.ActiveControl 為當前形式來更改焦點時,焦點事件會依以下順序發生:

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

當你用滑鼠或呼叫 Focus 方法改變焦點時,焦點事件會依以下順序發生:

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

CausesValidation 屬性設為 false,則 ValidatingValidated 事件會被抑制。

備註

EnterLeave事件被班級壓Form制。 該類別中 Form 對應的事件是 和 ActivatedDeactivate 事件。 EnterLeave事件是階層式的,會在母鏈上上下層遞減,直到達到適當的控制。 舉例來說,假設你有一個 Form 有兩個 GroupBox 控制點,每個 GroupBox 控制點都有一個 TextBox 控制點。 當插入點從一個TextBox移到另一個時,Leave事件會被提升到TextBoxEnterGroupBox另一個和,事件也會被提升到另一個GroupBoxTextBox和。

謹慎

不要試圖從 、 GotFocusLeaveLostFocusValidatingValidated 、 或事件處理者中Enter來設定焦點。 這樣做可能會導致你的應用程式或作業系統停止回應。 欲了解更多資訊,請參閱 WM_KILLFOCUS 主題。

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱