Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using TextBox_Leave or TextBox_LostFocus()?

Posted on 2014-10-30
3
Medium Priority
?
282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 34

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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 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