Control.Enter Event
Occurs when the control is entered.
[Visual Basic] Public Event Enter As EventHandler [C#] public event EventHandler Enter; [C++] public: __event EventHandler* Enter;
[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.
Event Data
The event handler receives an argument of type EventArgs.
Remarks
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:
When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:
If the CausesValidation property is set to false, the Validating and Validated events are suppressed.
Note The Enter and Leave events are suppressed by the Form class. The equivalent events in the Form class are the Activated and Deactivate events. The Enter and Leave events are hierarchical and will cascade up and down the parent chain until the appropriate control is reached. For example, assume you have a Form with two GroupBox controls, and each GroupBox control has one TextBox control. When the caret is moved from one TextBox to the other, the Leave event is raised for the TextBox and GroupBox, and the Enter event is raised for the other GroupBox and TextBox.
For more information about handling events, see Consuming Events.
Example
[Visual Basic] 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 [C#] 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); } [C++] 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); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also
Control Class | Control Members | System.Windows.Forms Namespace | OnEnter