表单聚焦时发生的事件

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

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

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); } } 

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

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

有一个Form.GotFocus事件。

GotFocus活动怎么样?

请注意,Control上的GotFocus事件(从中派生Form,因此它适用于此处)使用BrowsableAttribute标记,将值false传递给构造函数 ,因此在属性窗口中可见。

您应该在设计器生成的代码之外的代码中手动添加事件处理程序。

好吧,我不知道五年前,但现在:

在此处输入图像描述

Enter事件完成工作。