Control.Focus 方法

定義

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

public:
 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.

public:
   void ControlSetFocus( Control^ control )
   {
      
      // Set focus to the control, if it can receive focus.
      if ( control->CanFocus )
      {
         control->Focus();
      }
   }
public void ControlSetFocus(Control control)
{
   // Set focus to the control, if it can receive focus.
   if(control.CanFocus)
   {
      control.Focus();
   }
}
Public Sub ControlSetFocus(control As Control)
   ' Set focus to the control, if it can receive focus.
   If control.CanFocus Then
      control.Focus()
   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.

安全性

UIPermission
供所有視窗呼叫此方法。for all windows to call this method. 相關聯的列舉: UIPermissionWindowAllWindows 值。Associated enumeration: AllWindows value of UIPermissionWindow.

適用於

另請參閱