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 

事件类型

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

注解

使用 "键盘 (" 选项卡、"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 ,则将 Validating Validated 取消和事件。If the CausesValidation property is set to false, the Validating and Validated events are suppressed.

备注

EnterLeave 事件由 Form 类取消。The Enter and Leave events are suppressed by the Form class. 类中的等效事件 FormActivatedDeactivate 事件。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. 例如,假设有一个 Form 具有两个 GroupBox 控件的,并且每个 GroupBox 控件都有一个 TextBox 控件。For example, assume you have a Form with two GroupBox controls, and each GroupBox control has one TextBox control. 当插入符号从一个移 TextBox 到另一个时,将 Leave 为和引发事件, TextBoxGroupBox Enter 为另一个和引发事件 GroupBox TextBoxWhen 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 GotFocus Leave LostFocus ValidatingValidated 事件处理程序中设置焦点。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.

适用于

另请参阅