TextPointer.GetNextInsertionPosition Method
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Returns a TextPointer to the next insertion position in the specified logical direction.
Namespace: System.Windows.Documents
Assembly: System.Windows (in System.Windows.dll)
Syntax
'Declaration
Public Function GetNextInsertionPosition ( _
direction As LogicalDirection _
) As TextPointer
public TextPointer GetNextInsertionPosition(
LogicalDirection direction
)
Parameters
- direction
Type: System.Windows.Documents.LogicalDirection
One of the LogicalDirection values that specify the logical direction in which to search for the next insertion position.
Return Value
Type: System.Windows.Documents.TextPointer
A TextPointer that identifies the next insertion position in the requested direction, or nulla null reference (Nothing in Visual Basic) if no next insertion position can be found.
Remarks
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).
Examples
The following code uses the GetNextInsertionPosition method to underline the last word in the RichTextBox. This code example is part of a larger example used in the TextPointer class.
'This method underlines the last word in a RichTextBox
Public Sub UnderlineLastWord()
Dim EndofContent As TextPointer = MyRTB1.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward)
Dim currentPointer As TextPointer = EndofContent.GetNextInsertionPosition(LogicalDirection.Backward)
If (currentPointer Is Nothing) Then
Return
End If
Dim currentChar As String = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Backward)
While ((currentChar <> " ") _
AndAlso (currentChar <> ""))
currentPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Backward)
currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Backward)
End While
If (currentChar = " ") Then
MyRTB1.Selection.Select(currentPointer.GetNextInsertionPosition(LogicalDirection.Forward), EndofContent)
Else
MyRTB1.Selection.Select(currentPointer, EndofContent)
End If
MyRTB1.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline)
End Sub
Private Function GetCurrentChar(ByVal RTB As RichTextBox, ByVal pointer As TextPointer, ByVal direction As LogicalDirection) As String
Dim nextPointer As TextPointer = pointer.GetNextInsertionPosition(direction)
If (Not (nextPointer) Is Nothing) Then
RTB.Selection.Select(pointer, nextPointer)
If (RTB.Selection.Text.Length <> 0) Then
Return RTB.Selection.Text(0).ToString
End If
End If
Return ""
End Function
//This method underlines the last word in a RichTextBox
public void UnderlineLastWord()
{
TextPointer EndofContent = MyRTB1.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward);
TextPointer currentPointer = EndofContent.GetNextInsertionPosition(LogicalDirection.Backward);
if (currentPointer == null)
return;
string currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Backward);
while (currentChar != " " && currentChar != "")
{
currentPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Backward);
currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Backward);
}
if (currentChar == " ")
MyRTB1.Selection.Select(currentPointer.GetNextInsertionPosition(LogicalDirection.Forward), EndofContent);
else
MyRTB1.Selection.Select(currentPointer, EndofContent);
MyRTB1.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline);
}
private string GetCurrentChar(RichTextBox RTB, TextPointer pointer, LogicalDirection direction)
{
TextPointer nextPointer = pointer.GetNextInsertionPosition(direction);
if (nextPointer != null)
{
RTB.Selection.Select(pointer, nextPointer);
if (RTB.Selection.Text.Length != 0)
return RTB.Selection.Text[0].ToString();
}
return "";
}
Version Information
Silverlight
Supported in: 5, 4
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.