通过


Control.GotFocus 事件

定义

当控件收到焦点时发生。

public:
 event EventHandler ^ GotFocus;
[System.ComponentModel.Browsable(false)]
public event EventHandler GotFocus;
[System.ComponentModel.Browsable(false)]
public event EventHandler? GotFocus;
[<System.ComponentModel.Browsable(false)>]
member this.GotFocus : EventHandler 
Public Custom Event GotFocus As EventHandler 

事件类型

属性

示例

下面的代码示例演示了此成员的使用。 在此示例中,事件处理程序报告事件的发生情况 GotFocus 。 此报告可帮助你了解事件发生的时间,并可以帮助你进行调试。

若要运行示例代码,请将其粘贴到包含继承自 Control的类型实例的项目(如 ButtonComboBox)。 然后为实例 Control1 命名,并确保事件处理程序与 GotFocus 该事件相关联。

private void Control1_GotFocus(Object sender, EventArgs e) {

   MessageBox.Show("You are in the Control.GotFocus event.");
}
Private Sub Control1_GotFocus(sender as Object, e as EventArgs) _ 
     Handles Control1.GotFocus

Console.WriteLine("You are in the Control.GotFocus event.")

End Sub

注解

使用键盘(TAB、SHIFT+TAB 等)更改焦点时,通过调用 SelectSelectNextControl 方法,或通过将 ContainerControl.ActiveControl 属性设置为当前窗体来更改焦点时,焦点事件按以下顺序发生:

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

使用鼠标或调用 Focus 方法更改焦点时,焦点事件按以下顺序发生:

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

CausesValidation如果该属性设置为false,则Validating取消该属性和Validated事件。

注意事件GotFocusLostFocus是与WM_KILLFOCUS和WM_SETFOCUS Windows 消息绑定的低级别焦点事件。 通常,GotFocus仅当更新UICues或编写自定义控件时,才会使用和LostFocus事件。 Enter应将和Leave事件用于除使用ActivatedDeactivate事件的类以外的Form所有控件。 有关和LostFocus事件的详细信息GotFocus,请参阅WM_SETFOCUSWM_KILLFOCUS主题。

注意

请勿尝试从内部EnterGotFocusLeaveLostFocusValidatingValidated事件处理程序设置焦点。 这样做可能会导致应用程序或操作系统停止响应。 有关详细信息,请参阅 WM_KILLFOCUS 主题。

有关处理事件的详细信息,请参阅 处理和引发事件

适用于

另请参阅