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

后端 未结 2 1734
无人及你
无人及你 2021-02-05 01:57

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.

相关标签:
2条回答
  • 2021-02-05 02:50

    Control.Enter event happens when a control gets focus for the first time. While Control.GotFocus happens EVERY time a control gets focus. For example, you have 'textBox1' that already has focus and you call textBox1.Focus(), the GotFocus event will always fire in this instance, unlike for the Enter event that will only fire if a control doesn't already have the focus and receives it for the first time.

    0 讨论(0)
  • 2021-02-05 03:01

    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.

    0 讨论(0)
提交回复
热议问题