Mital Kakaiya [MCSD.NET]
In Programming, Logic is everything.

Never use LostFocus event

Sunday, June 12, 2005 7:36 PM

I was working with complex usercontrol with a grid and child usercontrols, which raises certain delegates (events). After in-depth code dubug, LostFocus event does not fire in same sequence, when control lost it's focus using KeyBoard or Mouse as specified in MSDN documentation.

The issue is:
When cancel property of the CancelEventArgs object is set to true in the Validating event delegate, all events that would normally occur after the Validating event are suppressed. It indicates that LostFocus fires "randomly" based on user's interaction with a control, so do not trust LostFocus event to raise everytime. Simply, As Microsoft recommanded, do not use LostFocus event. Alternatively use Leave, Validating or Validated events.




Feedback

# re: Never use LostFocus event

Hi all,


I'm trying to detect when a Form loses the focus. Of course LostFocus doesn't work. I tried Leave and Validated, but neither are fired. I'm no MCSD or expert, and I'm losing hope :( 9/28/2005 12:05 PM | Mutos

# re: Never use LostFocus event

Hi Mutos,

I did not get your complete question, but you want to fire an event when form loses its focus.

If It's an MDI application, you have to look for MDI Form's event as current form's control does not loose it's focus. Have a look at MDI form's Activated/Deactivated events.
hxxp://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsformclassdeactivatetopic.asp

If you want to fire an event when user click outside of the application, you need to use subclassing as follow:
hxxp://www.vbaccelerator.com/home/VB/Code/Libraries/Subclassing/Detecting_When_Another_Application_is_Activated/article.asp

Also:
hxxp://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsnativewindowclasstopic.asp

I hope this helps.

Regards,
Mital Kakaiya [MCSD.NET]
9/29/2005 10:56 AM | Mital Kakaiya [MCSD.NET]

Comments have been closed on this topic.