FindText Method

TextPatternRange.FindText Method

Returns a text range subset that contains the specified text.

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

public TextPatternRange FindText (
	string text,
	bool backward,
	bool ignoreCase
public TextPatternRange FindText (
	String text, 
	boolean backward, 
	boolean ignoreCase
public function FindText (
	text : String, 
	backward : boolean, 
	ignoreCase : boolean
) : TextPatternRange
Not applicable.



The text string to search for.


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


true if case should be ignored; otherwise false.

Return Value

A text range matching the specified text; otherwise null (Nothing in Microsoft Visual Basic .NET).

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


Use DocumentRange to search the entire document.

 private TextPatternRange TextFromSelection(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[] tprSelection = textpatternPattern.GetSelection();
    // Find 'text' in selection range
    return tprSelection[0].FindText("text", false, true);

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

Community Additions

© 2016 Microsoft