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 等) 、调用 SelectSelectNextControl 方法或将 属性设置为 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 设置为 falseValidating 则取消和 Validated 事件。

注意

EnterLeave 事件由 Form 类取消。 类中的 Form 等效事件是 ActivatedDeactivate 事件。 EnterLeave 事件是分层的,会在父链中上下级联,直到达到相应的控件。 例如,假设你有一个具有两GroupBoxForm控件的 ,并且每个GroupBox控件都有一个 TextBox 控件。 当插入点从一个插入点移动到另一个 TextBox 时, Leave 会为 TextBoxGroupBox引发 事件, Enter 并为另一个 GroupBoxTextBox引发 事件。

注意

不要尝试从 、、GotFocusValidatingLeaveLostFocus、 或 Validated 事件处理程序中Enter设置焦点。 这样做可能会导致应用程序或操作系统停止响应。 有关详细信息,请参阅 WM_KILLFOCUS 主题。

有关处理事件的详细信息,请参阅 处理和引发事件

适用于

另请参阅