Automation.RemoveAutomationEventHandler Method (AutomationEvent, AutomationElement, AutomationEventHandler)

 

Removes the specified UI Automation event handler.

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

Public Shared Sub RemoveAutomationEventHandler (
	eventId As AutomationEvent,
	element As AutomationElement,
	eventHandler As AutomationEventHandler
)

Parameters

eventId
Type: System.Windows.Automation.AutomationEvent

An event identifier.

element
Type: System.Windows.Automation.AutomationElement

The UI Automation element on which to remove the event handler.

eventHandler
Type: System.Windows.Automation.AutomationEventHandler

The handler method that was passed to AddAutomationEventHandler for the specified event identifier and UI Automation element.

The following example code shows an event handler being added and removed.

' 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
    Try
        sourceElement = DirectCast(src, AutomationElement)
    Catch ex As ElementNotAvailableException
        Exit Sub
    End Try
    If e.EventId Is InvokePattern.InvokedEvent Then
        ' TODO Add handling code.
    Else
    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

.NET Framework
Available since 3.0
Return to top
Show: