在我的用户控件中找不到 GotFocus()/LostFocus() [英] Can't Find GotFocus()/LostFocus() in My User Control

查看:28
本文介绍了在我的用户控件中找不到 GotFocus()/LostFocus()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a WinForms User Control. I read a couple of places something about GotFocus() and LostFocus() events, yet my user control doesn't provide these events in the Events part of the Properties window.

我什至尝试输入 override 来查看这些事件处理程序是否会出现,但它们不会.我在任何地方都找不到它们.

I even tried typing override to see if these event handlers would come up but they don't. I can't find them anywhere.

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

So I created my own methods with these names, and then I get the following error:

警告 1 'mynamespace.mycontrol.GotFocus()' 隐藏继承成员 'System.Windows.Forms.Control.GotFocus'.如果要隐藏,请使用 new 关键字.

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

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

What the heck is going on here. If GotFocus() already exists, why can't I find it and use it?

推荐答案

它看起来像来自 MSDN 文档 表明它们是从 Control 继承的,但不鼓励使用.他们希望您使用 Enter 和 Leave 事件.

It looks like from the MSDN Documentation that they are there inherited from Control, but are not encouraged to be used. They want you to use the Enter and Leave Events.

注意 GotFocus 和 LostFocus 事件是与 WM_KILLFOCUS 和 WM_SETFOCUS Windows 消息相关联的低级焦点事件.通常,GotFocus 和 LostFocus 事件仅在更新 UICues 或编写自定义控件时使用.除了使用 Activated 和 Deactivate 事件的 Form 类之外,所有控件都应该使用 Enter 和 Leave 事件.

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 事件.

That said you can get access to them as User1718294 suggested with the += or you can Override the OnGotFocus and OnLostFocus Event.

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

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

这篇关于在我的用户控件中找不到 GotFocus()/LostFocus()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆