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 事件。 相反地, EnterLeave 事件應該用於 除了 使用 ActivatedDeactivate 事件的 類別以外的 Form 所有控制項。 如需 和 LostFocus 事件的詳細資訊 GotFocus ,請參閱WM_SETFOCUSWM_KILLFOCUS主題。

警告

請勿嘗試從 、、、、 ValidatingLostFocusValidated 事件處理常式內 Enter 設定焦點。 LeaveGotFocus 這樣做可能會導致您的應用程式或作業系統停止回應。 如需詳細資訊,請參閱 WM_KILLFOCUS 主題。

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱