Assuming you are using Windows Forms and not WPF ...
You can detect an 'Enter' keypress via the KeyPress event.
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then MessageBox.Show("Enter") End If End Sub
That won't detect focus leaving the textbox via tab or mouse click etc though. For that you might have to use the Validating event.
Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
Dim isValid As Boolean
isValid = True ' Check what you need to check here.
If isValid = False Then e.Cancel = True End If
End Sub