This documentation is archived and is not being maintained.

Control.CanFocus Property

Gets a value indicating whether the control can receive focus.

[Visual Basic]
Public ReadOnly Property CanFocus As Boolean
[C#]
public bool CanFocus {get;}
[C++]
public: __property bool get_CanFocus();
[JScript]
public function get CanFocus() : Boolean;

Property Value

true if the control can receive focus; otherwise, false.

Remarks

In order for a control to receive input focus, the control must have a handle assigned to it, and the Visible and Enabled properties must both be set to true.

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

See Also

Control Class | Control Members | System.Windows.Forms Namespace | Enabled | Handle | Visible | Focus | Focused | CanSelect

Show: