共用方式為


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的型別實例的專案中,例如 a 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 等)、呼叫 Select or SelectNextControl 方法,或將屬性設 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,則 ValidatingValidated 事件會被抑制。

GotFocusLostFocus事件是低階焦點事件,與 WM_KILLFOCUS 和 WM_SETFOCUS Windows 訊息相關聯。 通常, GotFocusLostFocus 事件僅在更新 UICues 或撰寫自訂控制時使用。 相反地Enter,除了類別外,所有控制ActivatedForm都應該使用 and LeaveDeactivate 事件。 欲了解更多活動GotFocusLostFocus資訊,請參閱WM_SETFOCUSWM_KILLFOCUS主題。

謹慎

不要試圖從 、 GotFocusLeaveLostFocusValidatingValidated 、 或事件處理者中Enter來設定焦點。 這樣做可能會導致你的應用程式或作業系統停止回應。 欲了解更多資訊,請參閱 WM_KILLFOCUS 主題。

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

適用於

另請參閱