Control.Focus Method


为控件设置输入焦点。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 窗体控件不可选择。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.

Applies to

See also