开发者

Event to detect when a user control gains focus?

开发者 https://www.devze.com 2023-03-05 16:51 出处:网络
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

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 us开发者_JS百科e 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.


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.


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


I trick like this.

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

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

精彩评论

暂无评论...
验证码 换一张
取 消