How does the Windows Forms control know when its form was (de) activated?

I have a Windows Forms application in C # .NET. It contains a user-driven control that also handles keyboard focus. If part of the control has focus, a focus highlight frame will be highlighted around it. If the form containing the control is deactivated, it is obvious that the focus border should disappear from the control. But control does not even receive notifications about it. It receives the Leave event when another control focuses, and not another window. How can this control know about this?

+3
source share
1 answer

When Form + Control loads, Control can subscribe to the Activate and DeActivated forms of the event.

If it is a UserControl, for this you have a Control.Load event. For CustomControl, I would have to look for it.

In any case, be sure to implement Dispose in your control to unsubscribe from events.

Just tried:

private void UserControl1_FormActivate(object sender, EventArgs e)
{
    label1.Text = "Acitve";
}

private void UserControl1_FormDeActivate(object sender, EventArgs e)
{
    label1.Text = "InAcitve";
}

private void UserControl1_Load(object sender, EventArgs e)
{
    this.ParentForm.Activated += UserControl1_FormActivate;
    this.ParentForm.Deactivate += UserControl1_FormDeActivate;
}
+3
source

Source: https://habr.com/ru/post/1735238/


All Articles