gpt4 book ai didi

c# - 在我的用户控件中找不到 GotFocus()/LostFocus()

转载 作者:太空狗 更新时间:2023-10-30 00:11:20 26 4
gpt4 key购买 nike

我已经创建了一个 WinForms 用户控件。我阅读了一些关于 GotFocus()LostFocus() 事件的地方,但我的用户控件没有在“属性”窗口的“事件”部分提供这些事件。

我什至尝试输入 override 来查看这些事件处理程序是否会出现,但他们没有。我到处都找不到它们。

所以我用这些名称创建了自己的方法,然后出现以下错误:

Warning 1 'mynamespace.mycontrol.GotFocus()' hides inherited member 'System.Windows.Forms.Control.GotFocus'. Use the new keyword if hiding was intended.

这到底是怎么回事。如果 GotFocus() 已经存在,为什么我找不到它并使用它?

最佳答案

它看起来像来自 MSDN Documentation它们是从 Control 继承的,但不鼓励使用。他们希望您使用进入和离开事件。

Note The GotFocus and LostFocus events are low-level focus events that are tied to the WM_KILLFOCUS and WM_SETFOCUS Windows messages. Typically, the GotFocus and LostFocus events are only used when updating UICues or when writing custom controls. Instead the Enter and Leave events should be used for all controls except the Form class, which uses the Activated and Deactivate events.

也就是说,您可以按照 User1718294 使用 += 的建议访问它们,或者您可以覆盖 OnGotFocusOnLostFocus 事件。

protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
}

protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
}

关于c# - 在我的用户控件中找不到 GotFocus()/LostFocus(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13355140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com