Control.GotFocus 事件

定義

發生於控制項取得焦點時。Occurs when the control receives focus.

public:
 event EventHandler ^ GotFocus;
[System.ComponentModel.Browsable(false)]
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 事件應該用於所有控制項,但不包括使用 ActivatedDeactivate 事件的 Form 類別。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.

適用於

另請參閱