检测用户控件何时获得焦点的事件?

发布于 2024-11-06 14:35:27 字数 884 浏览 0 评论 0原文

我的表单上有一个用户控件,可以生成多个非模式子表单。子窗体显示用户可以编辑的数据。用户控件显示相同数据的不同视图,并且如果用户在一个或多个子表单中进行了更改,我希望能够重新加载数据。我已经设置了事件来通知用户控件子窗体中的数据更改,但我无法弄清楚应该在用户控件上捕获哪个事件(如果存在)以确定它已从子表格。我可以在这里使用现有的事件吗?

编辑:代码示例 (抱歉,我的客户喜欢 VB)

GotFocus 似乎仅在 UserControl 被处置时才会触发。

我将控件添加到“主”窗体上的面板,如下所示:

Dim mainControl as New MainUserControl()
Panel1.Controls.Add(mainControl)
mainControl.Doc = DocStyle.Fill
mainControl.Visible = True
mainControl.Show()

然后,事件处理程序代码:

Private Sub MainUserControl_GotFocus(ByVal sender as Object, ByVal e as EventArgs) Handles Me.GotFocus
    MessageBox.Show("got focus")
End Sub

然后,用户控件上有一个“关闭”按钮,该按钮将事件触发回主窗体,然后删除用户从面板控制并处理它。仅当调用 mainControl.Dispose() 时,GotFocus 才会触发。

编辑2
我刚刚尝试处理 Enter 事件,但这仅在第一次触发,而不是每次控件接收焦点时触发。

I have a user control on a form that can spawn several non-modal child forms. The child forms display data that the user can edit. The user control displays a different view of the same data, and I want to be able to spawn a reload of the data if the user has made changes in one or more of the child forms. I already have events set up to notify the user control of data changes in the child forms, but I can't figure out which event (if it exists) I should capture on the user control to determine that it has gotten focus back from the child form(s). Is there an existing event I can use here?

EDIT: Code sample
(sorry, my client likes VB)

GotFocus only seems to fire when the UserControl is Disposed.

I add the control to a panel on the "main" form like this:

Dim mainControl as New MainUserControl()
Panel1.Controls.Add(mainControl)
mainControl.Doc = DocStyle.Fill
mainControl.Visible = True
mainControl.Show()

Then, the event handler code:

Private Sub MainUserControl_GotFocus(ByVal sender as Object, ByVal e as EventArgs) Handles Me.GotFocus
    MessageBox.Show("got focus")
End Sub

Then, there's a "close" button on the user control that fires an event back to the main form, which then removes the user control from the panel and disposes it. Only when mainControl.Dispose() is called does GotFocus fire.

EDIT 2
I just tried handling the Enter event, but that only fires the first time and not every time the control receives focus.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一杯敬自由 2024-11-13 14:35:27

我能找到的最好方法是在用户控件中处理父窗体的 Activated 事件。从过去几个小时所做的所有研究中,我了解到 GotFocus 使用起来很棘手,而且 Winforms 用户控件根本不喜欢获得焦点,因为它是一个容器控件。假设我有一个用户控件 MyUserControl 加载到名为“MyControlParentForm”的表单上,那么我使用的代码如下所示:

Private Sub MyUserControl_Load(ByVal sender as Object, ByVal e As EventArgs) Handles MyBase.Load
    Dim parent = TryCast(ParentForm, MyControlParentForm)
    If parent IsNot Nothing Then
        AddHandler parent.Activated, AddressOf Control_Activated
    End If
End Sub

Private Sub Control_Activated(ByVal sender As Object, ByVal e as EventArgs)
    Debug.WriteLine("activated")
End Sub

然后,如果表单因任何原因失去焦点然后重新获得焦点MyUserControl 通过 Control_Activated 了解它。希望这对将来的人有帮助。

The best way to do this that I can find is by handling the parent form's Activated event in the user control. From all the research I've done over the last few hours, I've learned that GotFocus is a tricky thing to work with, and that the Winforms user control doesn't like to have focus at all, because it's a container control. Say I have a user control MyUserControl that is loaded onto a form called 'MyControlParentForm', then the code I'm using is something like this:

Private Sub MyUserControl_Load(ByVal sender as Object, ByVal e As EventArgs) Handles MyBase.Load
    Dim parent = TryCast(ParentForm, MyControlParentForm)
    If parent IsNot Nothing Then
        AddHandler parent.Activated, AddressOf Control_Activated
    End If
End Sub

Private Sub Control_Activated(ByVal sender As Object, ByVal e as EventArgs)
    Debug.WriteLine("activated")
End Sub

Then, if the form loses focus for any reason and then regains it, MyUserControl knows about it through Control_Activated. Hopefully this helps someone in the future.

猫烠⑼条掵仅有一顆心 2024-11-13 14:35:27

c# 中有一个事件称为 Control.GotFocus Event。在.net 2.0之前不存在。

There is an event in c# called Control.GotFocus Event. does not exist before .net 2.0.

江湖正好 2024-11-13 14:35:27

我就是这样骗人的

protected override void OnVisibleChanged(EventArgs e)
{
    base.OnVisibleChanged(e);

    if (Disposing == false)
    {
        if (Visible == true)
        {
            // OnShown(e);
        }
        else
        {
            // OnHide(e);
        }
    }
}

I trick like this.

protected override void OnVisibleChanged(EventArgs e)
{
    base.OnVisibleChanged(e);

    if (Disposing == false)
    {
        if (Visible == true)
        {
            // OnShown(e);
        }
        else
        {
            // OnHide(e);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文