Control.Focus Control.Focus Control.Focus Control.Focus Method

定義

設定控制項的輸入焦點。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方法會傳回true如果控制項成功收到輸入的焦點。The 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.

控制項可以選取和接收輸入的焦點,如果所有下列都條件成立:Selectable的值ControlStyles設定為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 Form 控制項不能選取。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. 相關聯的列舉型別:AllWindows的值UIPermissionWindowAssociated enumeration: AllWindows value of UIPermissionWindow.

適用於

另請參閱