Export (0) Print
Expand All

TextPatternRange.ExpandToEnclosingUnit Method

Expands the text range to the specified TextUnit.

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

public void ExpandToEnclosingUnit(
	TextUnit unit
)

Parameters

unit
Type: System.Windows.Automation.Text.TextUnit
The textual unit.

If the range is already an exact quantity of the specified units then it remains unchanged.

In order for the ExpandToEnclosingUnit method to execute successfully, a sequence of actions is performed behind the scenes.

  1. The text range is normalized; that is, the text range is collapsed to a degenerate range at the Start endpoint, making the End endpoint superfluous. This step is necessary to remove ambiguity in situations where a text range spans unit boundaries; for example, "{The U}RL is embedded in text" where "{" and "}" are the text range endpoints.

  2. The resulting range is moved backward in the DocumentRange to the beginning of the requested unit boundary.

  3. The range is moved forward or backward in the DocumentRange by the requested number of unit boundaries.

  4. The range is then expanded from a degenerate range state by moving the End endpoint by one requested unit boundary.

 

Examples of how a text range is adjusted for Move() and ExpandToEnclosingUnit()

Range adjustments by Move & ExpandToEnclosingUnit
NoteNote

These steps are necessary since it is common for a screen reader to read out a full word, sentence, or entire paragraph at the insertion point or any virtual cursor position.

ExpandToEnclosingUnit respects both hidden and visible text. The UI Automation client can check the IsHiddenAttribute for text visibility.

ExpandToEnclosingUnit defers to the next largest TextUnit supported if the given TextUnit is not supported by the control.

The order, from smallest unit to largest, is listed below.


 private void ExpandSelection(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;
    }
    TextPatternRange[] currentSelection = textpatternPattern.GetSelection();
    // Expand selection to include entire document
    currentSelection[0].ExpandToEnclosingUnit(TextUnit.Document);
}



Private Sub ExpandSelection(ByVal target As AutomationElement)
    ' Specify the control type we're looking for, in this case 'Document'
    Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)

    ' target --> The root AutomationElement.
    Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)

    Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)

    If (textpatternPattern Is Nothing) Then
        Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
        Return
    End If
    Dim currentSelection As TextPatternRange() = textpatternPattern.GetSelection()
    currentSelection(0).ExpandToEnclosingUnit(TextUnit.Document)
End Sub


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2015 Microsoft