Control.GotFocus Event


在控件接收焦点时发生。Occurs when the control receives focus.

 event EventHandler ^ GotFocus;
public event EventHandler GotFocus;
member this.GotFocus : EventHandler 
Public Custom Event GotFocus As EventHandler 


下面的代码示例演示如何使用此成员。The following code example demonstrates the use of this member. 在此示例中,事件处理程序报告 GotFocus 事件的发生。In the example, an event handler reports on the occurrence of the GotFocus event. 此报表可帮助你了解何时发生事件,并可帮助你进行调试。This report helps you to learn when the event occurs and can assist you in debugging.

若要运行示例代码,请将其粘贴到一个项目中,该项目包含继承自 Control的类型的实例,如 ButtonComboBoxTo run the example code, paste it into a project that contains an instance of a type that inherits from Control, such as a Button or ComboBox. 然后,将该实例命名为 Control1 并确保事件处理程序与 GotFocus 事件关联。Then name the instance Control1 and ensure that the event handler is associated with the GotFocus event.

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 属性设置为当前窗体,焦点事件会按以下顺序发生:When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

使用鼠标或通过调用 Focus 方法更改焦点时,焦点事件将按以下顺序发生:When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:

  1. Enter

  2. GotFocus

  3. LostFocus

  4. Leave

  5. Validating

  6. Validated

如果 CausesValidation 属性设置为 false,则将取消 ValidatingValidated 事件。If the CausesValidation property is set to false, the Validating and Validated events are suppressed.

注意GotFocusLostFocus 事件是与 WM_KILLFOCUS 和 WM_SETFOCUS Windows 消息相关联的低级别焦点事件。Note The GotFocus and LostFocus events are low-level focus events that are tied to the WM_KILLFOCUS and WM_SETFOCUS Windows messages. 通常,仅当更新 UICues 或编写自定义控件时,才使用 GotFocusLostFocus 事件。Typically, the GotFocus and LostFocus events are only used when updating UICues or when writing custom controls. 相反,EnterLeave 事件应该用于除 Form 类之外的所有控件,后者使用 ActivatedDeactivate 事件。Instead the Enter and Leave events should be used for all controls except the Form class, which uses the Activated and Deactivate events. 有关 GotFocusLostFocus 事件的详细信息,请参阅WM_SETFOCUSWM_KILLFOCUS主题。For more information about the GotFocus and LostFocus events, see the WM_SETFOCUS and WM_KILLFOCUS topics.


不要尝试从 EnterGotFocusLeaveLostFocusValidatingValidated 事件处理程序中设置焦点。Do not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers. 这样做可能会导致您的应用程序或操作系统停止响应。Doing so can cause your application or the operating system to stop responding. 有关详细信息,请参阅WM_KILLFOCUS主题。For more information, see the WM_KILLFOCUS topic.

有关处理事件的详细信息,请参阅处理和引发事件For more information about handling events, see Handling and Raising Events.

Applies to

See also