Raise Events from a UI Automation Provider

This topic contains example code that shows how to raise an event from a UI Automation provider.

Example

In the following example, a UI Automation event is raised in the implementation of a custom button control. The implementation enables a UI Automation client application to simulate a button click.

To avoid unnecessary processing, the example checks ClientsAreListening to see whether events should be raised.

''' <summary> 
''' Responds to a button click, regardless of whether it was caused by a  
''' mouse or keyboard click or by InvokePattern.Invoke.  
''' </summary> 
Private Sub OnCustomButtonClicked()

    '' TODO  Perform program actions invoked by the control. 

    '' Raise an event. 
    If AutomationInteropProvider.ClientsAreListening Then 
        Dim args As AutomationEventArgs = _
            New AutomationEventArgs(InvokePatternIdentifiers.InvokedEvent)
        AutomationInteropProvider.RaiseAutomationEvent( _
            InvokePatternIdentifiers.InvokedEvent, Me, args)
    End If 
End Sub
/// <summary> 
/// Responds to a button click, regardless of whether it was caused by a mouse or 
/// keyboard click or by InvokePattern.Invoke.  
/// </summary> 
private void OnCustomButtonClicked()
{
    // TODO  Perform program actions invoked by the control. 

    // Raise an event. 
    if (AutomationInteropProvider.ClientsAreListening)
    {
        AutomationEventArgs args = new AutomationEventArgs(InvokePatternIdentifiers.InvokedEvent);
        AutomationInteropProvider.RaiseAutomationEvent(InvokePatternIdentifiers.InvokedEvent, this, args);
    }
}

See Also

Concepts

UI Automation Providers Overview