Control.Leave 事件

定义

在输入焦点离开控件时发生。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

注解

使用键盘(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

如果 CausesValidation 属性设置为 false,则将取消 ValidatingValidated 事件。If the CausesValidation property is set to false, the Validating and Validated events are suppressed.

备注

Form 类抑制 EnterLeave 事件。The Enter and Leave events are suppressed by the Form class. Form 类中的等效事件是 ActivatedDeactivate 事件。The equivalent events in the Form class are the Activated and Deactivate events. EnterLeave 事件具有层次结构,并将在父链上向上和向下,直到达到适当的控件。The Enter and Leave events are hierarchical and will cascade up and down the parent chain until the appropriate control is reached. 例如,假设有一个具有两个 GroupBox 控件的 Form,并且每个 GroupBox 控件都有一个 TextBox 控件。For example, assume you have a Form with two GroupBox controls, and each GroupBox control has one TextBox control. 当插入符号从一个 TextBox 移动到另一个时,将为 TextBoxGroupBox引发 Leave 事件,并为另一个 EnterGroupBox 引发 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.

注意

不要尝试从 EnterGotFocusLeaveLostFocusValidatingValidated 事件处理程序中设置焦点。Do 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.

适用于

另请参阅