Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

AutomationElement.GetCurrentPattern Method

Retrieves the specified pattern object on this AutomationElement.

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

public Object GetCurrentPattern (
	AutomationPattern pattern
)
public Object GetCurrentPattern (
	AutomationPattern pattern
)
public function GetCurrentPattern (
	pattern : AutomationPattern
) : Object
Not applicable.

Parameters

pattern

The identifier of the pattern to retrieve.

Return Value

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

InvalidOperationException

The pattern is not supported by the element.

ElementNotAvailableException

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

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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.