WinForms event for TextBox focus?

this.tGID.GotFocus += OnFocus;
this.tGID.LostFocus += OnDefocus;

private void OnFocus(object sender, EventArgs e)
{
   MessageBox.Show("Got focus.");
}

private void OnDefocus(object sender, EventArgs e)
{
    MessageBox.Show("Lost focus.");
}

This should do what you want and this article describes the different events that are called and in which order. You might see a better event.


You're looking for the GotFocus event. There is also a LostFocus event.

textBox1.GotFocus += textBox1_GotFocus;

I up-voted Hans Passant's comment, but it really should be an answer. I'm working on a Telerik UI in a 3.5 .NET environment, and there is no GotFocus Event on a RadTextBoxControl. I had to use the Enter event.

textBox1.Enter += textBox1_Enter;

Tags:

C#

.Net

Winforms