Export (0) Print
Expand All

KeyboardFocusChangedEventArgs Class

Provides data for the GotKeyboardFocus and LostKeyboardFocus events.

Namespace: System.Windows.Input
Assembly: PresentationCore (in presentationcore.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

'Declaration
Public Class KeyboardFocusChangedEventArgs
	Inherits KeyboardEventArgs
'Usage
Dim instance As KeyboardFocusChangedEventArgs

public class KeyboardFocusChangedEventArgs extends KeyboardEventArgs
public class KeyboardFocusChangedEventArgs extends KeyboardEventArgs
You cannot use this managed class in XAML.

KeyboardFocusChangedEventArgs is used with the following events: LostKeyboardFocus, GotKeyboardFocus, and the corresponding tunneling events.

If the PreviewGotKeyboardFocus event or the PreviewLostKeyboardFocus event is handled, keyboard focus will not change.

This example shows how to change the color of an element when it gains and loses focus by using the GotFocus and LostFocus events.

This example consists of a Extensible Application Markup Language (XAML) file and a code-behind file. For the complete samples, see Firing Events when an Element Gains and Loses Focus Sample.

The following XAML creates the user interface, which consists of two Button objects, and attaches event handlers for the GotFocus and LostFocus events to the Button objects.

<StackPanel>
  <StackPanel.Resources>
    <Style TargetType="{x:Type Button}">
      <Setter Property="Height" Value="20"/>
      <Setter Property="Width" Value="250"/>
      <Setter Property="HorizontalAlignment" Value="Left"/>
    </Style>
  </StackPanel.Resources>
  <Button
      GotFocus="OnGotFocusHandler"
      LostFocus="OnLostFocusHandler">Click Or Tab To Give Keyboard Focus</Button>
  <Button
      GotFocus="OnGotFocusHandler"
      LostFocus="OnLostFocusHandler">Click Or Tab To Give Keyborad Focus</Button>
</StackPanel>

The following code behind creates the GotFocus and LostFocus event handlers. When the Button gains keyboard focus, the Background of the Button is changed to red. When the Button loses keyboard focus, the Background of the Button is changed back to white.

Partial Public Class Window1
    Inherits Window

    Public Sub New()
        InitializeComponent()
    End Sub

    'raised when Button gains focus. Changes the color of the Button to red.
    Sub OnGotFocusHandler(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim tb As Button = CType(e.Source, Button)
        tb.Background = Brushes.Red
    End Sub

    'raised when Button loses focus. Changes the color back to white.
    Sub OnLostFocusHandler(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim tb As Button = CType(e.Source, Button)
        tb.Background = Brushes.White
    End Sub
End Class

System.Object
   System.EventArgs
     System.Windows.RoutedEventArgs
       System.Windows.Input.InputEventArgs
         System.Windows.Input.KeyboardEventArgs
          System.Windows.Input.KeyboardFocusChangedEventArgs

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

ADD
Show:
© 2014 Microsoft