【问题标题】:What is the difference between the Control.Enter and Control.GotFocus events?Control.Enter 和 Control.GotFocus 事件有什么区别?
【发布时间】:2011-02-11 17:21:28
【问题描述】:

这可能是一个基本问题,但我不得不承认我从来没有真正理解过 Control.Enter 和 Control.GotFocus 事件之间的区别。

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

这是捕获键盘或鼠标输入还是其他东西之间的区别?

【问题讨论】:

    标签: windows winforms user-interface events


    【解决方案1】:

    GotFocus/LostFocus 事件分别由 Windows 消息 WM_SETFOCUS 和 WM_KILLFOCUS 生成。它们有点麻烦,尤其是容易出现死锁的 WM_KILLFOCUS。 Windows 窗体中处理验证逻辑(例如验证事件)的逻辑可以覆盖焦点更改。换句话说,焦点实际上发生了变化,但随后验证代码将其移回。您的 UI 的逻辑状态是它从未移动过,您不应该意识到它移动了。

    Enter/Leave 事件避免了这些低级焦点更改通知事件可能导致的麻烦,它们是在 Winforms 建立真正的焦点时生成的。你几乎总是想使用这些。

    【讨论】:

      【解决方案2】:

      Control.Enter 事件在控件第一次获得焦点时发生。而 Control.GotFocus 每次控件获得焦点时都会发生。例如,你有'textBox1',它已经有焦点并且你调用了textBox1.Focus(),GotFocus 事件总是会在这个实例中触发,不像Enter 事件只会在控件没有焦点时触发并第一次收到。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-12
        • 2013-03-04
        • 2010-11-09
        • 2013-08-29
        • 1970-01-01
        • 2022-07-28
        • 2012-01-27
        相关资源
        最近更新 更多