Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the Control.Enter and Control.GotFocus events?

This may be a basic question, but I have to admit I've never truly understood what the difference between the Control.Enter and Control.GotFocus events is.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.enter.aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.control.gotfocus.aspx

Is it a differentiation between capturing keyboard or mouse input or something else?

like image 469
jameswelle Avatar asked Apr 23 '10 21:04

jameswelle


People also ask

What is GotFocus event?

The GotFocus event occurs after the Enter event. If you move the focus to a control on a form, and that control doesn't have the focus on that form, the Exit and LostFocus events for the control that does have the focus on the form occur before the Enter and GotFocus events for the control you moved to.

Which event is executed first for form control?

The Form and Control classes expose a set of events related to application startup and shutdown. When a Windows Forms application starts, the startup events of the main form are raised in the following order: Control. HandleCreated.

Which event is executed when form receives the focus?

The GotFocus event occurs when the specified object receives the focus.


1 Answers

The GotFocus/LostFocus events are generated by Windows messages, WM_SETFOCUS and WM_KILLFOCUS respectively. They are a bit troublesome, especially WM_KILLFOCUS which is prone to deadlock. The logic inside Windows Forms that handles the validation logic (Validating event for example) can override focus changes. In other words, the focus actually changed but then the validation code moved it back. The logical state of your UI is that it never moved and you shouldn't be aware that it did.

The Enter/Leave events avoid the kind of trouble these low-level focus change notification events can cause, they are generated when Winforms has established the true focus. You almost always want to use these.

like image 95
Hans Passant Avatar answered Sep 16 '22 22:09

Hans Passant