Control.Enter 事件
进入控件时发生。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
Public Event Enter As EventHandler
用法
Dim instance As Control
Dim handler As EventHandler
AddHandler instance.Enter, handler
public event EventHandler Enter
public:
event EventHandler^ Enter {
void add (EventHandler^ value);
void remove (EventHandler^ value);
}
/** @event */
public void add_Enter (EventHandler value)
/** @event */
public void remove_Enter (EventHandler value)
JScript 支持使用事件,但不支持进行新的声明。
备注
当通过使用键盘(Tab、Shift+Tab 等)、通过调用 Select 或 SelectNextControl 方法或者通过将 ContainerControl.ActiveControl 属性设置为当前窗体等方式更改焦点时,焦点事件按以下顺序发生:
当通过使用鼠标或调用 Focus 方法的方式更改焦点时,焦点事件按以下顺序发生:
Enter
GotFocus
LostFocus
Leave
Validating
Validated
如果 CausesValidation 属性设置为 false,则将取消 Validating 和 Validated 事件。
提示
Enter 和 Leave 事件被 Form 类取消。Form 类中的等效事件为 Activated 和 Deactivate 事件。Enter 和 Leave 事件是分层的,将沿父级链上下级联,直到到达相应的控件为止。例如,假定您有一个带有两个 GroupBox 控件的 Form,每个 GroupBox 控件有一个 TextBox 控件。将插入符号从一个 TextBox 移至另一个时,将对移出的 TextBox 和 GroupBox 引发 Leave 事件,而对移入的 GroupBox 和 TextBox 引发 Enter 事件。
有关处理事件的更多信息,请参见 使用事件。
示例
下面的代码示例使用 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