Focus Method
TOC
Collapse the table of content
Expand the table of content

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.

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 Language Filter 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: 

See Also

Control Class | Control Members | System.Windows.Forms Namespace | CanFocus | ContainsFocus | Focused | CanSelect

Show:
© 2016 Microsoft