sniff_bits
sniff_bits

Reputation: 409

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.

Upvotes: 6

Views: 12431

Answers (3)

Amit Tiwari
Amit Tiwari

Reputation: 368

you can use jquery...

<input id="txtName" type="text" />

<script type="text/javascript">
    $("#txtName").blur(function () {
        alert("I am not in textbox.");
    });
</script>

Upvotes: -4

Linga
Linga

Reputation: 10573

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 

Upvotes: 14

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101742

Also there is a LostFocus event to do this:

private void txtbox_LostFocus(object sender, EventArgs e)
{
    //your Code
} 

Upvotes: 4

Related Questions