Solved

Using TextBox_Leave or TextBox_LostFocus()?

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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