Control.OnEnter 方法
引发 Enter 事件。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
Protected Overridable Sub OnEnter ( _
e As EventArgs _
)
用法
Dim e As EventArgs
Me.OnEnter(e)
protected virtual void OnEnter (
EventArgs e
)
protected:
virtual void OnEnter (
EventArgs^ e
)
protected void OnEnter (
EventArgs e
)
protected function OnEnter (
e : EventArgs
)
参数
- e
包含事件数据的 EventArgs。
备注
引发事件时会通过委托调用事件处理程序。有关更多信息,请参见 引发事件。
OnEnter 方法还允许派生类对事件进行处理而不必附加委托。这是在派生类中处理事件的首选技术。
给继承者的说明 在派生类中重写 OnEnter 时,一定要调用基类的 OnEnter 方法,以便已注册的委托对事件进行接收。
示例
下面的代码示例使用 Enter 事件在特殊条件下更改 TextBox 的前景色和背景色。
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