了解 Winforms 应用程序中面板是否失去焦点的事件?

发布于 2024-11-27 18:01:15 字数 217 浏览 1 评论 0原文

我有一个简单的表单,其中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把昨日还给我 2024-12-04 18:01:15

我认为 LostFocus 就是您正在寻找的。

编辑

作为另一种策略,您知道调用 panel.BringToFront 会在 UI 中对更新进行排队。无论您在何处调用panel.BringToFront,也许您都可以只调用您自己的方法之一,或触发您自己的事件之一。这样,您知道事件何时被触发,以及确切什么会触发它。

我想到这一点的原因是我怀疑您的 Panel 本身是否真的具有焦点 - 相反,它的子控件之一可能会具有焦点。通过创建自己的事件触发器,您不必依赖像焦点这样不稳定的东西。另外,即使 Panel 确实获得了焦点,它也总是有可能以除您自己的面板切换之外的其他方式失去焦点。

编辑#2

这是快速实现我之前的漫无目的的尝试。我将假设此代码放置在与所有 Panel 实例相同的类中的某个位置(即在您的 Form 类中)。

// This will be the custom event to which you can subscribe
// in order to detect a switch in panels.
public event EventHandler PanelSwapEvent;

// This reference the currently visible panel - should be set
// to the default panel in the form's constructor, if possible.
private Panel currentPanel;

// This actually switches the panels, to minimize code duplication.
private void switchToPanel(Panel p)
{
    Panel lastPanel = currentPanel;
    currentPanel = p;

    // Move the panels, and invoke the event.

    p.BringToFront();
    if(PanelSwapEvent != null)
        PanelSwapEvent(lastPanel, new EventArgs());
}

// Here's the actual event handler (replaces your
// pnlServiceInfo_LostFocus handler).
private void PanelSwapHandler(object sender, EventArgs e)
{
    // whatever you want to do when panels are swapped
}

在此示例中,事件处理程序的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 calling panel.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 the Panel 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 your Form class).

// This will be the custom event to which you can subscribe
// in order to detect a switch in panels.
public event EventHandler PanelSwapEvent;

// This reference the currently visible panel - should be set
// to the default panel in the form's constructor, if possible.
private Panel currentPanel;

// This actually switches the panels, to minimize code duplication.
private void switchToPanel(Panel p)
{
    Panel lastPanel = currentPanel;
    currentPanel = p;

    // Move the panels, and invoke the event.

    p.BringToFront();
    if(PanelSwapEvent != null)
        PanelSwapEvent(lastPanel, new EventArgs());
}

// Here's the actual event handler (replaces your
// pnlServiceInfo_LostFocus handler).
private void PanelSwapHandler(object sender, EventArgs e)
{
    // whatever you want to do when panels are swapped
}

In this example, the sender of the event handler is the panel that lost "focus". Using it is as simple as saying switchToPanel(pnl_whatever) to indicate that you would like to switch from the current panel to the panel named pnl_whatever.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文