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之類型實例的專案中, 例如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

如果屬性設定為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除了Leave 使用Activated和事件的類別之外, 所有控制項都應該使用和事件。 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 LostFocus 、、Validated或事件處理常式中設定焦點。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.