Control.Leave 事件

在输入焦点离开控件时发生。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Event Leave As EventHandler
用法
Dim instance As Control
Dim handler As EventHandler

AddHandler instance.Leave, handler
public event EventHandler Leave
public:
event EventHandler^ Leave {
    void add (EventHandler^ value);
    void remove (EventHandler^ value);
}
/** @event */
public void add_Leave (EventHandler value)

/** @event */
public void remove_Leave (EventHandler value)
JScript 支持使用事件,但不支持进行新的声明。

备注

当通过使用键盘(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 属性设置为 false,则将取消 ValidatingValidated 事件。

提示

EnterLeave 事件被 Form 类取消。Form 类中的等效事件为 ActivatedDeactivate 事件。EnterLeave 事件是分层的,将沿父级链上下级联,直到到达相应的控件为止。例如,假定您有一个带有两个 GroupBox 控件的 Form,每个 GroupBox 控件有一个 TextBox 控件。将插入符号从一个 TextBox 移至另一个时,将对移出的 TextBoxGroupBox 引发 Leave 事件,而对移入的 GroupBoxTextBox 引发 Enter 事件。

有关处理事件的更多信息,请参见 使用事件

示例

下面的代码示例使用 Leave 事件将控件重置为其以前的状态。

    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 'textBox1_Enter
   
   
    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 'textBox1_Leave
End Class 'Form1 
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);
    }
}

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:
   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 (!(textBox1.get_Text().Equals(""))) {
        textBox1.set_ForeColor(Color.get_Red());
        textBox1.set_BackColor(Color.get_Black());

        // Move the selection pointer to the end of the text of the 
        // control.
        textBox1.Select(textBox1.get_Text().get_Length(), 0);
    }
} //textBox1_Enter

private void textBox1_Leave(Object sender, System.EventArgs e)
{
    // Reset the colors and selection of the TextBox after focus is lost.
    textBox1.set_ForeColor(Color.get_Black());
    textBox1.set_BackColor(Color.get_White());
    textBox1.Select(0, 0);
} //textBox1_Leave

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Control 类
Control 成员
System.Windows.Forms 命名空间
OnLeave
Control.Enter 事件