UI Automation Events for Clients

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 describes how Microsoft UI Automation events are used by UI Automation clients.

UI Automation allows clients to subscribe to events of interest. This capability improves performance by eliminating the need to continually poll all the UI elements in the system to see if any information, structure, or state has changed.

Efficiency is also improved by the ability to listen for events only within a defined scope. For example, a client can listen for focus change events on all UI Automation elements in the tree, or on just one element and its descendants.

Note

Do not assume that all possible events are raised by a Microsoft UI Automation provider. For example, not all property changes cause events to be raised by the standard proxy providers for Windows Forms and Win32 controls.

For a broader view of UI Automation events, see UI Automation Events Overview.

Subscribing to Events

Client applications subscribe to events of a particular kind by registering an event handler, using one of the following methods.

Method Event Type Event Arguments Type Delegate Type
AddAutomationFocusChangedEventHandler Focus change AutomationFocusChangedEventArgs AutomationFocusChangedEventHandler
AddAutomationPropertyChangedEventHandler Property change AutomationPropertyChangedEventArgs AutomationPropertyChangedEventHandler
AddStructureChangedEventHandler Structure change StructureChangedEventArgs StructureChangedEventHandler
AddAutomationEventHandler All other events, identified by an AutomationEvent AutomationEventArgs or WindowClosedEventArgs AutomationEventHandler

Before calling the method, you must create a delegate method to handle the event. If you prefer, you can handle different kinds of events in a single method, and pass this method in multiple calls to one of the methods in the table. For example, a single AutomationEventHandler can be set up to handle various events differently according to the EventId.

Note

To process window-closed events, cast the argument type that is passed to the event handler as WindowClosedEventArgs. Because the Microsoft UI Automation element for the window is no longer valid, you cannot use the sender parameter to retrieve information; use GetRuntimeId instead.

Caution

If your application might receive events from its own UI, do not use your application's UI thread to subscribe to events, or to unsubscribe. Doing so might lead to unpredictable behavior. For more information, see UI Automation Threading Issues.

On shutdown, or when UI Automation events are no longer of interest to the application, UI Automation clients should call one of the following methods.

Method Description
RemoveAutomationEventHandler Unregisters an event handler that was registered by using AddAutomationEventHandler.
RemoveAutomationFocusChangedEventHandler Unregisters an event handler that was registered by using AddAutomationFocusChangedEventHandler.
RemoveAutomationPropertyChangedEventHandler Unregisters an event handler that was registered by using AddAutomationPropertyChangedEventHandler.
RemoveAllEventHandlers Unregisters all registered event handlers.

For example code, see Subscribe to UI Automation Events.

See also