An event that occurs when a form is focused

I have two forms: frmBase, and the second is frmBalloon. I change the focus of both forms, which are displayed frmBase first, then frmBalloon is displayed (frmBase is not displayed), and then frmBase is displayed again. Now I need an event that happens with the first frmBase load, and then again when it displays after frmBalloon becomes invisible.

So, I need an event that occurs when the form becomes focused .......

+3
source share
4 answers

Is Form.Activatedwhat you after?

, GotFocus , , . :

using System;
using System.Drawing;
using System.Windows.Forms;

class Test
{
    static void Main()
    {

        TextBox tb = new TextBox();
        Button button = new Button
        {
            Location = new Point(0, 30),
            Text = "New form"
        };
        button.Click += (sender, args) =>
        {
            string name = tb.Text;
            Form f = new Form();
            f.Controls.Add(new Label { Text = name });
            f.Activated += (s, a) => Console.WriteLine("Activated: " + name);
            f.GotFocus += (s, a) => Console.WriteLine("GotFocus: " + name);
            f.Show();
            f.Controls.Add(new TextBox { Location = new Point(0, 30) });
        };

        Form master = new Form { Controls = { tb, button } };
        Application.Run(master);
    }
}

( - , .)

- " ", . - , Activated , GotFocus.

+15
+1

GotFocus?

Note that the GotFocus event in the control (from which the form is derived, which is why it is used here) is marked BrowsableAttribute , passing false to the constructor so it does not appear in the properties window.

You must add the event handler manually to the code outside the code created by the constructor.

+1
source

Well, I do not know five years ago, but now:

enter image description here

The event Enteris completing a task.

0
source

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


All Articles