How do I capture lost focus on a Textbox?
c#, textbox, winforms
Solution
You can use `Leave` event
private void txtbox_Leave(object sender, EventArgs e)
{
//your Code
}
You can also use,
private void txtbox_LostFocus(object sender, EventArgs e)
{
//your Code
}
`Leave()` event first executes keyboard event and then executes mouse event where as `LostFocus()` event first executes mouse event and then executes keyboard event.
Basically When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), the events occurred in the following order
1. Enter
2. GotFocus
3. Leave
4. Validating
5. Validated
6. LostFocus
When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:
1. Enter
2. GotFocus
3. LostFocus
4. Leave
5. Validating
6. Validated
Problem
I have a texbox tbx1, when I have the cursor blinking on the textbox, when I click the mouse on some other control I want to display a message, But the issue is I have to use an event of the textbox tbx1 to capture that focus change.