AutomationElement.GetCurrentPattern Method (AutomationPattern)


Retrieves the specified pattern object on this AutomationElement.

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

Public Function GetCurrentPattern (
	pattern As AutomationPattern
) As Object


Type: System.Windows.Automation.AutomationPattern

The identifier of the pattern to retrieve.

Return Value

Type: System.Object

The pattern object, if the specified pattern is currently supported by the AutomationElement.

Exception Condition

The pattern is not supported by the element.


The UI for the AutomationElement no longer exists.

GetCurrentPattern gets the specified pattern based on its availability at the time of the call.

For some forms of UI, this method will incur cross-process performance overhead. Applications can concentrate overhead by caching patterns and then retrieving them by using GetCachedPattern.

The following example shows how to use this method to retrieve a SelectionItemPattern, which is then used to select an item in a list box.

''' <summary>
''' Sets the focus to a list and selects a string item in that list.
''' </summary>
''' <param name="listElement">The list element.</param>
''' <param name="itemText">The text to select.</param>
''' <remarks>
''' This deselects any currently selected items. To add the item to the current selection 
''' in a multiselect list, use AddToSelection instead of Select.
''' </remarks>
Public Sub SelectListItem(ByVal listElement As AutomationElement, ByVal itemText As String)
    If listElement Is Nothing OrElse itemText = "" Then
        Throw New ArgumentException("Argument cannot be null or empty.")
    End If
    Dim cond As New PropertyCondition(AutomationElement.NameProperty, itemText, PropertyConditionFlags.IgnoreCase)
    Dim elementItem As AutomationElement = listElement.FindFirst(TreeScope.Children, cond)
    If Not (elementItem Is Nothing) Then
        Dim pattern As SelectionItemPattern
            pattern = DirectCast(elementItem.GetCurrentPattern(SelectionItemPattern.Pattern), _
        Catch ex As InvalidOperationException
            Console.WriteLine(ex.Message) ' Most likely "Pattern not supported."
        End Try
    End If

End Sub 'SelectListItem

For often-repeated tasks such as the one in the example, it would be more efficient to cache the pattern and use GetCachedPattern.

.NET Framework
Available since 3.0
Return to top