Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I capture lost focus on a Textbox?

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.

like image 626
sniff_bits Avatar asked Jan 31 '14 06:01

sniff_bits


People also ask

What is LostFocus?

The LostFocus event occurs after the Exit event. If you move the focus to a control on a form, and that control doesn't have the focus on that form, the Exit and LostFocus events for the control that does have the focus on the form occur before the Enter and GotFocus events for the control you moved to.

What is lost focus in VB6?

VB6 controls fire the Validate event first and then the LostFocus event; if the Validate sets Cancel=True, then the LostFocus event is never fired. The sequence is the same regardless of how the end user moves the input focus away from the control.

What is leave event in C#?

Net. A very useful event you can use for text boxes is the Leave event. It allows you to validate a text box when a user tries to leave it. You can check, for example, if the text box is blank.


1 Answers

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 
like image 76
Linga Avatar answered Sep 21 '22 22:09

Linga