Control.GotFocus Control.GotFocus Control.GotFocus 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继承的类型的实例,如ComboBox Button或。To 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

如果将false Validating属性设置为,则将取消Validated和事件。 CausesValidationIf 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. 通常,仅GotFocusLostFocus更新UICues或编写自定义控件时,才使用和事件。Typically, the GotFocus and LostFocus events are only used when updating UICues or when writing custom controls. Form Deactivate Activated相反, Leave和事件应该用于除类之外的所有控件,后者使用和事件。 EnterInstead 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.


Enter不要尝试从GotFocusLeave 、、Validated 、或事件处理程序中设置焦点。 LostFocus ValidatingDo 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.