Solved

Using TextBox_Leave or TextBox_LostFocus()?

Posted on 2014-10-30
3
178 Views
Last Modified: 2014-10-30
What is the difference between the TextBox Leave() Event and the LostFocus() Event and when should each of them be used? This would apply for a ComboBox control as well.

Thanks!
0
Comment
Question by:BlakeMcKenna
3 Comments
 
LVL 33

Expert Comment

by:it_saige
ID: 40414121
LostFocus occurs when the control (in this case a TextBox) loses focus.  Leave occurs when the input focus leaves the control.

How you change focus also determines the order of the firing events, - Per MSDN:
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur 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

If the CausesValidation property is set to false, the Validating and Validated events are suppressed.
Control.Leave
Control.LostFocus

In those same msdn whitepages.  Microsoft states that you should use Enter and Leave as opposed to GotFocus and LostFocus becasue they are low-level focus events tied to the WM_KILLFOCUS and WM_SETFOCUS windows messages.  And that they are typically used when updating UICues or when writing custom controls.

https://social.msdn.microsoft.com/Forums/en-US/dd023378-d700-4c5f-a5b5-072fd4de7903/lostfocus-vs-leave-events?forum=Vsexpressvb

-saige-
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40414160
As stated in the documentation and pointed to by it_saige, Leave is recommended.

And even if this is what is suggested, if you look carefully at when the events are triggered, this also makes sense most of the time.

If the cursor is in the control and your get out of it to go somewhere else on the same form, both events are triggered. But if instead you move the cursor to another form, only LostFocus is triggered, because you did not leave the control. The cursor is still in it when you come back to the form (unless you do so by clicking on another control in the same form). Most of the time, you do not want to react to that. It is simply that the user wants to look up something or change something in another form.
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40414305
Thanks guys...I appreciate the input!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question