Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using TextBox_Leave or TextBox_LostFocus()?

Posted on 2014-10-30
3
Medium Priority
?
323 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 35

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

926 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