Control.OnEnter(EventArgs) 方法

定义

引发 Enter 事件。

C#
protected virtual void OnEnter(EventArgs e);
C#
protected internal virtual void OnEnter(EventArgs e);

参数

e
EventArgs

包含事件数据的 EventArgs

示例

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

C#
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);
}

注解

引发事件时,将通过委托调用事件处理程序。 有关详细信息,请参阅 处理和引发事件

OnEnter 方法还允许派生类对事件进行处理而不必附加委托。 这是在派生类中处理事件的首选技术。

继承者说明

在派生类中重写 OnEnter(EventArgs) 时,一定要调用基类的 OnEnter(EventArgs) 方法,以便已注册的委托对事件进行接收。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅