了解 Winforms 应用程序中面板是否失去焦点的事件?
我有一个简单的表单,其中有 4 个面板。每个面板都停靠在父面板中,以确保在给定时间只有一个面板可见。现在,对于Panel2,当它从前移到后时,我想处理该事件。我通过调用 panel.BringToFront()
使面板可见 我尝试过 Leave
事件,但不起作用。对于Form,事件是Deactivate
,对于Panel,事件是什么?
I have a simple Form with 4 panels in it. Each of those panels are docked in the parent, to ensure only one is visible at a given time. Now, for Panel2, when it is moving from front to back, I would like to work on that event. I am making panels visible by calling panel.BringToFront()
I have tried Leave
event but that doesn't work. For Form, the event is Deactivate
, what's the event for Panel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为
LostFocus
就是您正在寻找的。编辑
作为另一种策略,您知道调用
panel.BringToFront
会在 UI 中对更新进行排队。无论您在何处调用panel.BringToFront,也许您都可以只调用您自己的方法之一,或触发您自己的事件之一。这样,您知道事件何时被触发,以及确切什么会触发它。我想到这一点的原因是我怀疑您的
Panel
本身是否真的具有焦点 - 相反,它的子控件之一可能会具有焦点。通过创建自己的事件触发器,您不必依赖像焦点这样不稳定的东西。另外,即使Panel
确实获得了焦点,它也总是有可能以除您自己的面板切换之外的其他方式失去焦点。编辑#2
这是快速实现我之前的漫无目的的尝试。我将假设此代码放置在与所有
Panel
实例相同的类中的某个位置(即在您的Form
类中)。在此示例中,事件处理程序的
sender
是失去“焦点”的面板。使用它就像说switchToPanel(pnl_whatever)
一样简单,表明您想要从当前面板切换到名为pnl_whatever
的面板。I'm thinking
LostFocus
is what you're looking for.Edit
As another strategy, you know that calling
panel.BringToFront
will queue an update in your UI. Wherever you are callingpanel.BringToFront
, perhaps you could just call one of your own methods, or trigger one of your own events. This way, you know when the event will be triggered, and exactly what will trigger it.The reason I thought of this is that I doubt your
Panel
will ever actually have the focus itself - rather, one of its child controls will likely have the focus. By doing you own event trigger, you don't have to rely on something as volatile as focus. Plus, even if thePanel
did have the focus, it's always possible that it could lose focus in other ways than your own panel switching.Edit #2
Here's an attempt at a quick implementation of my previous ramblings. I'll be making the assumption that this code be placed somewhere in the same class as all your
Panel
instances (i.e. in yourForm
class).In this example, the
sender
of the event handler is the panel that lost "focus". Using it is as simple as sayingswitchToPanel(pnl_whatever)
to indicate that you would like to switch from the current panel to the panel namedpnl_whatever
.