Control.Focus 方法


設定控制項的輸入焦點。Sets input focus to the control.

 bool Focus();
public bool Focus ();
member this.Focus : unit -> bool
Public Function Focus () As Boolean


如果輸入焦點要求成功,則為 true,否則為 falsetrue if the input focus request was successful; otherwise, false.


下列程式碼範例會將焦點設為指定的 Control(如果可以接收焦點)。The following code example sets focus to the specified Control, if it can receive focus.

   void ControlSetFocus( Control^ control )
      // Set focus to the control, if it can receive focus.
      if ( control->CanFocus )
public void ControlSetFocus(Control control)
   // Set focus to the control, if it can receive focus.
Public Sub ControlSetFocus(control As Control)
   ' Set focus to the control, if it can receive focus.
   If control.CanFocus Then
   End If
End Sub


如果控制項成功收到輸入焦點,則 Focus 方法會傳回 trueThe Focus method returns true if the control successfully received input focus. 控制項可以有輸入焦點,而不會顯示任何具有焦點的視覺提示。The control can have the input focus while not displaying any visual cues of having the focus. 這項行為主要是由下面所列的其控制項或任何衍生自它們的控制項所觀察到。This behavior is primarily observed by the nonselectable controls listed below, or any controls derived from them.

如果下列所有條件都成立,則可以選取控制項並接收輸入焦點: ControlStylesSelectable 值設定為 [true]、包含在另一個控制項中,而且其所有父控制項都是可見和啟用的。A control can be selected and receive input focus if all the following are true: the Selectable value of ControlStyles is set to true, it is contained in another control, and all its parent controls are both visible and enabled.

下列清單中的 Windows Forms 控制項無法選取。The Windows Forms controls in the following list are not selectable. 這些控制項衍生的控制項也無法選取。Controls derived from these controls are also not selectable.


Focus 是主要用於自訂控制項作者的低層級方法。Focus is a low-level method intended primarily for custom control authors. 相反地,應用程式設計人員應該使用子控制項的 Select 方法或 ActiveControl 屬性,或表單的 Activate 方法。Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.