Solved

Using TextBox_Leave or TextBox_LostFocus()?

Posted on 2014-10-30
3
152 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 32

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now