通过


Control.Enter 事件

定义

输入控件时发生。

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

事件类型

示例

下面的代码示例使用 Enter 事件来更改特定条件下的前景色和背景色 TextBox

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如果该属性设置为false,则Validating取消该属性和Validated事件。

注释

Enter取消Form了事件和Leave事件。 类中的 Form 等效事件是 ActivatedDeactivate 事件。 事件Leave是分层的Enter,将向上和向下级联父链,直到达到相应的控件。 例如,假设你有一个包含两GroupBoxForm控件,每个GroupBox控件都有一个TextBox控件。 当插入符号从一个移动到另一个TextBox插入点时,Leave将引发GroupBoxTextBox该事件,并且为另一GroupBoxTextBox个和Enter引发该事件。

注意

请勿尝试从内部EnterGotFocusLeaveLostFocusValidatingValidated事件处理程序设置焦点。 这样做可能会导致应用程序或操作系统停止响应。 有关详细信息,请参阅 WM_KILLFOCUS “键盘输入参考”部分中的主题和 “关于消息和消息队列 ”主题的“消息死锁”部分。

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

适用于

另请参阅