This documentation is archived and is not being maintained.

TextPointer.GetInsertionPosition Method

Returns a TextPointer to the closest insertion position in a specified logical direction.

Namespace: System.Windows.Documents
Assembly: PresentationFramework (in presentationframework.dll)

public TextPointer GetInsertionPosition (
	LogicalDirection direction
public TextPointer GetInsertionPosition (
	LogicalDirection direction
public function GetInsertionPosition (
	direction : LogicalDirection
) : TextPointer
You cannot use methods in XAML.



A logical direction in which to search for the closest insertion position.

Return Value

A TextPointer to the closest insertion position in the specified direction.

An insertion position is a position where new content may be added without breaking any semantic rules for the associated content. In practice, an insertion position is anywhere in content where a caret may be positioned. An example of a valid TextPointer position that is not an insertion position is the position between two adjacent Paragraph tags (that is, between the closing tag of the preceding paragraph and the opening tag of the next paragraph).

If the TextPointer already points to a valid insertion position, but the closing tag for a non-empty formatting sequence directly follows that position in the given direction, then the TextPointer returned by this method is adjusted to point to the insertion position just after the close of the formatting sequence. For example, consider the markup sequence <Bold>a</Bold>b. Note that there are two insertion positions between the letters a and b - one that is inside of the closing Bold tag, and one directly following the closing Bold tag. If GetInsertionPosition is called on a TextPointer to the position directly after the letter a and inside of the closing Bold tag, and with a direction of Forward, the returned TextPointer will be adjusted to point to the position just before the letter b, after the closing Bold tag. A similar adjustment is made for opening formatting tags when working in the opposite logical direction. This method is intended to provide a means of disambiguation between insertion positions in similar cases.

This method can also be used to be selective about insertion points when a sequence of structural tags is involved. For example a position between closing and opening paragraph tags, the direction parameter can be used to select the closes insertion point at the beginning of the following paragraph (by specifying Forward) or at the end of the preceding paragraph (by specifying Backward).

If the pointer is already at insertion position, and there are no adjacent formatting tags in the specified direction, the returned TextPointer will point to the same position as the calling TextPointer.

It is possible that no valid insertion position exists relative to the position pointed to by a TextPointer. This can happen if the referenced content is structurally incomplete, as in an empty Table or List. In such cases, this method simply returns the a TextPointer to the same position as the TextPointer from which this method was called. This method always returns a valid TextPointer.

This example shows how to use the GetInsertionPosition method to check whether or not a specified is empty of printable content.

// Tests to see if the specified TextElement is empty (has no printatble content).
bool IsElementEmpty(TextElement element)
    // Find starting and ending insertion positions in the element.
    // Inward-facing directions are used to make sure that insertion position
    // will be found correctly in case when the element may contain inline 
    // formatting elements (such as a Span or Run that contains Bold or Italic elements).
    TextPointer start = element.ContentStart.GetInsertionPosition(LogicalDirection.Forward);
    TextPointer end = element.ContentEnd.GetInsertionPosition(LogicalDirection.Backward);
    // The element has no printable content if its first and last insertion positions are equal.
    return start.CompareTo(end) == 0;

} // End IsEmptyElement method.

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