This documentation is archived and is not being maintained.

TextPatternRange.MoveEndpointByUnit Method

Moves one endpoint of the text range the specified number of TextUnits within the document range.

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

public int MoveEndpointByUnit(
	TextPatternRangeEndpoint endpoint,
	TextUnit unit,
	int count
)

Parameters

endpoint
Type: System.Windows.Automation.Text.TextPatternRangeEndpoint
The endpoint to move.
unit
Type: System.Windows.Automation.Text.TextUnit
The textual unit for moving.
count
Type: System.Int32
The number of units to move. A positive count moves the endpoint forward. A negative count moves backward. A count of 0 has no effect.

Return Value

Type: System.Int32
The number of units actually moved, which can be less than the number requested if moving the endpoint runs into the beginning or end of the document.

When it is necessary to traverse the content of a text range, a series of steps are involved behind the scenes in order for the Move method to execute successfully.

  1. The text range is normalized; that is, the text range is collapsed to a degenerate range at the Start endpoint, which makes 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 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

The textual content (or inner text) of a text container and an embedded object, such as a hyperlink or table cell, is exposed as a single, continuous text stream in both the control view and the content view of the UI Automation tree; object boundaries are ignored. If a UI Automation client is retrieving the text for the purpose of reciting, interpreting, or analyzing in some manner, the text range should be checked for special cases, such as a table with textual content or other embedded objects. This can be accomplished by calling GetChildren to obtain an AutomationElement for each embedded object and then calling RangeFromChild to obtain a text range for each element; this is done recursively until all textual content has been retrieved.

 

Example of a text stream with embedded objects and their range spans

Text ranges spanned by embedded objects.

MoveEndpointByUnit 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 Int32 MoveEndpointByRangeFromSelection(AutomationElement target, Int32 units)
{
    // 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 0;
    }
    TextPatternRange[] currentSelection = textpatternPattern.GetSelection();

    return currentSelection[0].MoveEndpointByUnit(
        TextPatternRangeEndpoint.Start, TextUnit.Paragraph, units);
}



Private Function MoveEndpointByRangeFromSelection(ByVal target As AutomationElement, ByVal units As Int32) As Int32
    ' 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 Nothing
    End If
    Dim currentSelection As TextPatternRange() = textpatternPattern.GetSelection()
    ' GetText(-1) retrieves all characters but can be inefficient
    Return currentSelection(0).MoveEndpointByUnit(TextPatternRangeEndpoint.Start, TextUnit.Paragraph, units)
End Function


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