Edit

Share via


TextPatternRange.FindAttribute Method

Definition

Returns a text range subset that has the specified attribute value.

C#
public System.Windows.Automation.Text.TextPatternRange FindAttribute(System.Windows.Automation.AutomationTextAttribute attribute, object value, bool backward);

Parameters

attribute
AutomationTextAttribute

The attribute to search for.

value
Object

The attribute value to search for. This value must match the type specified for the attribute.

backward
Boolean

true if the last occurring text range should be returned instead of the first; otherwise false.

Returns

A text range having a matching attribute and attribute value; otherwise null (Nothing in Visual Basic).

Examples

C#
 private TextPatternRange RangeFromAttribute(AutomationElement target)
{
    // Specify the control type we're looking for, in this case 'Document'
    PropertyCondition cond = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document);

    // target --> The root AutomationElement.
    AutomationElement textProvider = target.FindFirst(TreeScope.Descendants, cond);

    TextPattern textpatternPattern = textProvider.GetCurrentPattern(TextPattern.Pattern) as TextPattern;

    if (textpatternPattern == null)
    {
        Console.WriteLine("Root element does not contain a descendant that supports TextPattern.");
        return null;
    }
    TextPatternRange[] currentSelelction = textpatternPattern.GetSelection();
    // Find 'italic' range
    return currentSelelction[0].FindAttribute(TextPattern.IsItalicAttribute, true, false);
}

Remarks

There is no differentiation between hidden and visible text. UI Automation clients can use IsHiddenAttribute to check text visibility.

Note

Use DocumentRange to search the entire document.

Applies to

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also