Control.GotFocus 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當控制裝置被聚焦時發生。
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 Button 或 ComboBox。 接著命名實例 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 為當前形式來更改焦點時,焦點事件會依以下順序發生:
當你用滑鼠或呼叫 Focus 方法改變焦點時,焦點事件會依以下順序發生:
若 CausesValidation 屬性設為 false,則 Validating 與 Validated 事件會被抑制。
註和GotFocusLostFocus事件是低階焦點事件,與 WM_KILLFOCUS 和 WM_SETFOCUS Windows 訊息相關聯。 通常, GotFocus 和 LostFocus 事件僅在更新 UICues 或撰寫自訂控制時使用。 相反地Enter,除了類別外,所有控制ActivatedForm都應該使用 and LeaveDeactivate 事件。 欲了解更多活動GotFocusLostFocus資訊,請參閱WM_SETFOCUS及WM_KILLFOCUS主題。
謹慎
不要試圖從 、 GotFocus、 Leave、 LostFocusValidatingValidated 、 或事件處理者中Enter來設定焦點。 這樣做可能會導致你的應用程式或作業系統停止回應。 欲了解更多資訊,請參閱 WM_KILLFOCUS 主題。
如需處理事件的詳細資訊,請參閱 處理和引發事件。