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.

如果满足以下所有条件,则可以选择一个控件并接收输入焦点: SelectableControlStyles值设置为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 窗体控件不可选择。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方法ActiveControlActivate子控件的属性,或使用窗体的方法。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 UIPermissionWindow枚举: 值。Associated enumeration: AllWindows value of UIPermissionWindow.

适用于

另请参阅