This documentation is archived and is not being maintained.

Automation.AddStructureChangedEventHandler Method

Registers the method that will handle structure-changed events.

Namespace: System.Windows.Automation
Assembly: UIAutomationClient (in uiautomationclient.dll)

public static void AddStructureChangedEventHandler (
	AutomationElement element,
	TreeScope scope,
	StructureChangedEventHandler eventHandler
)
public static void AddStructureChangedEventHandler (
	AutomationElement element, 
	TreeScope scope, 
	StructureChangedEventHandler eventHandler
)
public static function AddStructureChangedEventHandler (
	element : AutomationElement, 
	scope : TreeScope, 
	eventHandler : StructureChangedEventHandler
)
Not applicable.

Parameters

element

The UI Automation element with which to associate the event handler.

scope

The scope of events to be handled; that is, whether they are on the element itself, or on its ancestors and descendants.

eventHandler

The method to call when the structure-changed event occurs.

eventHandler can be an instance of the method, or a reference to the method (AddressOf in Visual Basic).

The following example shows a structure-changed event handler delegate that will be called whenever the subtree of the specified AutomationElement changes.

/// <summary>
/// Handles structure-changed events. If a new app window has been added, this method ensures
/// it's in the list of runtime IDs and subscribed to window-close events.
/// </summary>
/// <param name="sender">Object that raised the event.</param>
/// <param name="e">Event arguments.</param>
/// <remarks>
/// An exception can be thrown by the UI Automation core if the element disappears
/// before it can be processed -- for example, if a menu item is only briefly visible. 
/// This exception cannot be caught here because it crosses native/managed boundaries. 
/// In the debugger, you can ignore it and continue execution. The exception does not cause
/// a break when the executable is being run.
/// </remarks>
private void OnStructureChanged(object sender, StructureChangedEventArgs e)
{
    AutomationElement element = sender as AutomationElement;

    if (e.StructureChangeType == StructureChangeType.ChildAdded)
    {
        Object windowPattern;
        if (false == element.TryGetCurrentPattern(WindowPattern.Pattern, out windowPattern))
        {
            return;
        }
        int[] rid = e.GetRuntimeId();
        if (RuntimeIdListed(rid, savedRuntimeIds) < 0)
        {
            AddToWindowHandler(element);
            savedRuntimeIds.Add(rid);
        }

    }
}

The following example code adds an instance of the delegate.

// elementRoot is an AutomationElement.
Automation.AddStructureChangedEventHandler(elementRoot, TreeScope.Children, 
    new StructureChangedEventHandler(OnStructureChanged));

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
Show: