Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms event for TextBox focus?

Tags:

c#

.net

winforms

I want to add an even to the TextBox for when it has focus. I know I could do this with a simple textbox1.Focus and check the bool value... but I don't want to do it that way.

Here is how I would like to do it:

this.tGID.Focus += new System.EventHandler(this.tGID_Focus);

I'm not sure if EventHandler is the correct way to do this, but I do know that this does not work.

like image 626
mawburn Avatar asked Apr 21 '12 01:04

mawburn


2 Answers

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

textBox1.GotFocus += textBox1_GotFocus;
like image 160
lordcheeto Avatar answered Oct 19 '22 15:10

lordcheeto


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.

like image 38
Michael J. Gray Avatar answered Oct 19 '22 15:10

Michael J. Gray