.NET Framework Class Library
AutomationElement..::.GetCurrentPattern Method

Retrieves the specified pattern object on this AutomationElement.

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

Visual Basic (Declaration)
Public Function GetCurrentPattern ( _
    pattern As AutomationPattern _
) As Object
Visual Basic (Usage)
Dim instance As AutomationElement
Dim pattern As AutomationPattern
Dim returnValue As Object

returnValue = instance.GetCurrentPattern(pattern)
C#
public Object GetCurrentPattern(
    AutomationPattern pattern
)
Visual C++
public:
Object^ GetCurrentPattern(
    AutomationPattern^ pattern
)
JScript
public function GetCurrentPattern(
    pattern : AutomationPattern
) : Object

Parameters

pattern
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.
Exceptions

ExceptionCondition
InvalidOperationException

The pattern is not supported by the element.

ElementNotAvailableException

The UI for the AutomationElement no longer exists.

Remarks

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(Automationpattern).

Examples

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.

Visual Basic
''' <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
    listElement.SetFocus()
    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
        Try
            pattern = DirectCast(elementItem.GetCurrentPattern(SelectionItemPattern.Pattern), _
                SelectionItemPattern)
        Catch ex As InvalidOperationException
            Console.WriteLine(ex.Message) ' Most likely "Pattern not supported."
            Return
        End Try
        pattern.Select()
    End If

End Sub 'SelectListItem
C#
/// <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 void SelectListItem(AutomationElement listElement, String itemText)
{
    if ((listElement == null) || (itemText == ""))
    {
        throw new ArgumentException("Argument cannot be null or empty.");
    }
    listElement.SetFocus();
    Condition cond = new PropertyCondition(
        AutomationElement.NameProperty, itemText, PropertyConditionFlags.IgnoreCase);
    AutomationElement elementItem = listElement.FindFirst(TreeScope.Children, cond);
    if (elementItem != null)
    {
        SelectionItemPattern pattern;
        try
        {
            pattern = elementItem.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine(ex.Message);  // Most likely "Pattern not supported."
            return;
        }
        pattern.Select();
    }
}
NoteNote:

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 Security

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0
See Also

Reference

Other Resources

Tags :


Page view tracker