Focus method
Collapse the table of content
Expand the table of content

Control.Focus method

Attempts to set the focus on the control.


Public Function Focus(
  value As FocusState 
) As Boolean



Type: FocusState

Specifies how focus was set, as a value of the enumeration.

Return value

Type: System.Boolean [.NET] | Platform::Boolean [C++]

true if focus was set to the control, or focus was already on the control. false if the control is not focusable.


You can't remove focus from a control by calling this method with FocusState.Unfocused as the parameter. This value is not allowed and causes an exception. To remove focus from a control, set focus to a different control.

You typically pass FocusState.Programmatic as the parameter to indicate the control obtained focus through a deliberate call to the Focus method. For example, if clicking an "Edit" button causes focus to be set on a TextBox, use the Programmatic focus state.

Pass FocusState.Pointer if you’re setting focus as the direct result of a pointer interaction. Pass FocusState.Keyboard as the parameter if you’re setting focus as a result of a keyboard interaction, like a tab sequence or key press. For example, if you’re implementing an ItemsControl and handle key presses to let the user move focus between items in the control, use the Keyboard focus state when you call Focus in your key press handler.


In Windows 8, when FocusState is Programmatic, the keyboard focus visual is shown even if the prior method of input was pointer. In Windows 8.1, when you call Focus(FocusState.Programmatic), the prior FocusState, either Pointer or Keyboard, is retained so that the correct focus visual is shown. This means that if you check the value of the FocusState property after you call Focus(FocusState.Programmatic), the FocusState property will have a value of either Pointer or Keyboard.

In an app that’s compiled for Windows 8, but run in Windows 8.1, the Windows 8 behavior is retained. The FocusState property value is Programmatic and the keyboard focus visual is shown.


Requirements (Windows 10 device family)

Device family

Universal, introduced version 10.0.10240.0

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0


Windows::UI::Xaml::Controls [C++]



Requirements (Windows 8.x and Windows Phone 8.x)

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]


Windows::UI::Xaml::Controls [C++]



See also

Keyboard interactions
Keyboard accessibility
Responding to the appearance of the on-screen keyboard sample



© 2016 Microsoft