Control.Focus Method
Sets input focus to the control.
[Visual Basic] Public Function Focus() As Boolean [C#] public bool Focus(); [C++] public: bool Focus(); [JScript] public function Focus() : Boolean;
Return Value
true if the input focus request was successful; otherwise, false.
Remarks
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.
A control can be selected and receive input focus if all the following are true: its ControlStyles.Selectable style bit is set to true, it is contained in another control, and all its parent controls are both visible and enabled.
The Windows Forms controls in the following list are not selectable. Controls derived from these controls are also not selectable.
- Panel
- GroupBox
- PictureBox
- ProgressBar
- Splitter
- Label
- LinkLabel (when there is no link present in the control)
Example
[Visual Basic, C#, C++] The following example sets focus to the specified Control, if it can receive focus.
[Visual Basic] 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 [C#] public void ControlSetFocus(Control control) { // Set focus to the control, if it can receive focus. if(control.CanFocus) { control.Focus(); } } [C++] public: void ControlSetFocus(Control* control) { // Set focus to the control, if it can receive focus. if (control->CanFocus) { control->Focus(); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
.NET Framework Security:
- UIPermission for all windows to call this method. Associated enumeration: UIPermissionWindow.AllWindows
See Also
Control Class | Control Members | System.Windows.Forms Namespace | CanFocus | ContainsFocus | Focused | CanSelect