Occurs when the input focus leaves the control.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
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:
The Enter and events are suppressed by the Form class. The equivalent events in the Form class are the Activated and Deactivate events. The Enter and 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 event is raised for the TextBox and GroupBox, and the Enter event is raised for the other GroupBox and TextBox.
Do not attempt to set focus from within the Enter, GotFocus, , LostFocus, Validating, or Validated event handlers. Doing so can cause your application or the operating system to stop responding. For more information, see the WM_KILLFOCUS topic in the "Keyboard Input Reference" section, and the "Message Deadlocks" section of the "About Messages and Message Queues" topic in the MSDN library at http://msdn.microsoft.com/library.
For more information about handling events, see Consuming Events.
The following code example uses the event to reset a control to its former state.
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
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.