Control.Leave Control.Leave Control.Leave Control.Leave Event

定義

發生於輸入焦點離開控制項時。Occurs when the input focus leaves the control.

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

範例

下列程式碼範例會使用Leave事件,將控制項重設為其先前的狀態。The following code example uses the Leave event to reset a control to its former state.

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 'Form1 

備註

當您使用鍵盤(TAB、SHIFT + tab 等等)來變更焦點時,藉由呼叫SelectSelectNextControl方法ContainerControl.ActiveControl ,或藉由將屬性設定為目前的表單,焦點事件會依照下列順序發生:When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

當您使用滑鼠或藉由呼叫Focus方法來變更焦點時,焦點事件會依照下列順序發生:When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

如果屬性設定為false,則Validating會隱藏和Validated事件。 CausesValidationIf the CausesValidation property is set to false, the Validating and Validated events are suppressed.

注意

類別會Leave隱藏和事件。Enter FormThe Enter and Leave events are suppressed by the Form class. Form類別中的對等事件Activated是和Deactivate事件。The equivalent events in the Form class are the Activated and Deactivate events. EnterLeave事件是階層式的,並且會在父代鏈上向上和向下 cascade,直到達到適當的控制項為止。The Enter and Leave events are hierarchical and will cascade up and down the parent chain until the appropriate control is reached. 例如, Form假設您有一個具有兩個GroupBox控制項的,而且每個GroupBox控制項TextBox都有一個控制項。For example, assume you have a Form with two GroupBox controls, and each GroupBox control has one TextBox control. 當插入號移至另一個TextBoxLeave ,會針對TextBoxGroupBox引發事件,並Enter針對另GroupBox一個和TextBox引發事件。When the caret is moved from one TextBox to the other, the Leave event is raised for the TextBox and GroupBox, and the Enter event is raised for the other GroupBox and TextBox.

警告

Enter請勿嘗試從、 GotFocusLeave LostFocus 、、Validated或事件處理常式中設定焦點。ValidatingDo not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers. 這麼做可能會導致您的應用程式或作業系統停止回應。Doing so can cause your application or the operating system to stop responding. 如需詳細資訊,請參閱WM_KILLFOCUS主題。For more information, see the WM_KILLFOCUS topic.

如需處理事件的詳細資訊, 請參閱處理和引發事件For more information about handling events, see Handling and Raising Events.

適用於

另請參閱