Visual Basic Reference

LostFocus Event Example

This example changes the color of a TextBox control when it receives or loses the focus (selected with the mouse or TAB key) and displays the appropriate text in the Label control. To try this example, paste the code into the Declarations section of a form that contains two TextBox controls and a Label control, and then press F5 and move the focus between Text1 and Text2.

  Private Sub Text1_GotFocus ()
   ' Show focus with red.
   Text1.BackColor = RGB(255, 0, 0)
   Label1.Caption = "Text1 has the focus."
End Sub

Private Sub Text1_LostFocus ()
   ' Show loss of focus with blue.
   Text1.BackColor = RGB(0, 0, 255)
   Label1.Caption = "Text1 doesn't have the focus."
End Sub