Export (0) Print
Expand All

Subscribe to UI Automation Events

Note Note

This documentation is intended for .NET Framework developers who want to use the managed UI Automation classes defined in the System.Windows.Automation namespace. For the latest information about UI Automation, see Windows Automation API: UI Automation.

This topic shows how to subscribe to events raised by UI Automation providers.

The following example code registers an event handler for the event that is raised when a control such as a button is invoked, and removes it when the application form closes. The event is identified by an AutomationEvent passed as a parameter to AddAutomationEventHandler.

' Member variables. 
Private ElementSubscribeButton As AutomationElement
Private UIAeventHandler As AutomationEventHandler

''' <summary> 
''' Register an event handler for InvokedEvent on the specified element. 
''' </summary> 
''' <param name="elementButton">The automation element.</param> 
Public Sub SubscribeToInvoke(ByVal elementButton As AutomationElement)
    If (elementButton IsNot Nothing) Then
        UIAeventHandler = New AutomationEventHandler(AddressOf OnUIAutomationEvent)
        Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent, elementButton, _
        TreeScope.Element, UIAeventHandler)
        ElementSubscribeButton = elementButton
    End If 

End Sub 'SubscribeToInvoke

''' <summary> 
''' AutomationEventHandler delegate. 
''' </summary> 
''' <param name="src">Object that raised the event.</param> 
''' <param name="e">Event arguments.</param> 
Private Sub OnUIAutomationEvent(ByVal src As Object, ByVal e As AutomationEventArgs)
    ' Make sure the element still exists. Elements such as tooltips can disappear 
    ' before the event is processed. 
    Dim sourceElement As AutomationElement
        sourceElement = DirectCast(src, AutomationElement)
    Catch ex As ElementNotAvailableException
        Exit Sub 
    End Try 
    If e.EventId Is InvokePattern.InvokedEvent Then 
        ' TODO Add handling code. 
    End If 
    ' TODO Handle any other events that have been subscribed to.
    Console.WriteLine("Event: " & e.EventId.ProgrammaticName)
End Sub 'OnUIAutomationEvent

Private Sub ShutdownUIA()
    If (UIAeventHandler IsNot Nothing) Then
        Automation.RemoveAutomationEventHandler(InvokePattern.InvokedEvent, ElementSubscribeButton, UIAeventHandler)
    End If 

End Sub 'ShutdownUIA

The following example shows how to use Microsoft UI Automation to subscribe to an event that is raised when the focus changes. The event handler is unregistered in a method that could be called on application shutdown, or when notification of UI events is no longer required.

Private focusHandler As AutomationFocusChangedEventHandler = Nothing 

''' <summary> 
''' Create an event handler and register it. 
''' </summary> 
Public Sub SubscribeToFocusChange()
    focusHandler = New AutomationFocusChangedEventHandler(AddressOf OnFocusChange)

End Sub 'SubscribeToFocusChange

''' <summary> 
''' Handle the event. 
''' </summary> 
''' <param name="src">Object that raised the event.</param> 
''' <param name="e">Event arguments.</param> 
Private Sub OnFocusChange(ByVal src As Object, ByVal e As AutomationFocusChangedEventArgs)

End Sub 'OnFocusChange

' TODO Add event handling code. 
' The arguments tell you which elements have lost and received focus. 

''' <summary> 
''' Cancel subscription to the event. 
''' </summary> 
Public Sub UnsubscribeFocusChange()
    If (focusHandler IsNot Nothing) Then
    End If 

End Sub 'UnsubscribeFocusChange
© 2014 Microsoft