表单聚焦时发生的事件

Har*_*hna 7 .net c# winforms

我有两种形式,第一种是frmBase,第二种是frmBalloon.我改变两种形式的焦点,首先显示frmBase然后显示frmBalloon(frmBase不可见)然后再显示frmBase.现在我需要首先发生的事件frmBase加载,然后在frmBalloon变得不可见后显示.

所以我需要在表格变得集中时发生的事件.......

Jon*_*eet 25

Form.Activated你以后?

我之所以建议这样,而不是如果焦点从一种形式变为另一种形式的控制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);
    }
}
Run Code Online (Sandbox Code Playgroud)

(将其构建为控制台应用程序 - 这是输出的位置.)

在文本框中输入一些名称,然后单击"新表单" - 然后再次执行.现在单击新表单上的文本框 - 您将看到Activated事件被触发,但不是GotFocus.