KeyboardFocusChangedEventHandler Delegate

Represents the method that will handle the LostKeyboardFocus and GotKeyboardFocus events.

Namespace: System.Windows.Input
Assembly: PresentationCore (in presentationcore.dll)

'Declaration
Public Delegate Sub KeyboardFocusChangedEventHandler ( _
	sender As Object, _
	e As KeyboardFocusChangedEventArgs _
)
'Usage
Dim instance As New KeyboardFocusChangedEventHandler(AddressOf HandlerMethod)
/** @delegate */
public delegate void KeyboardFocusChangedEventHandler (
	Object sender, 
	KeyboardFocusChangedEventArgs e
)
In XAML, you can use delegates but you cannot define your own.

Parameters

sender

The source of the event.

e

The event data.

The KeyboardFocusChangedEventHandler 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.

When you create a KeyboardFocusChangedEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event-handler delegates, see Events and Delegates.

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

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: