Represents a position within a RichTextBox.
Assembly: System.Windows (in System.Windows.dll)
Thetype exposes the following members.
|IsAtInsertionPosition||Gets a value that indicates whether the current position is an insertion.|
|LogicalDirection||Gets the logical direction associated with the current position, which is used to disambiguate content associated with the current position.|
|Parent||Gets the logical parent that contains the current position.|
|VisualParent||Gets the visual tree parent of the object.|
|CompareTo||Performs an ordinal comparison between the positions specified by the current and a second specified .|
|Equals(Object)||Determines whether the specified Object is equal to the current Object. (Inherited from Object.)|
|Finalize||Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)|
|GetCharacterRect||Returns a bounding box for content that borders the current in the specified logical direction.|
|GetHashCode||Serves as a hash function for a particular type. (Inherited from Object.)|
|GetNextInsertionPosition||Returns a to the next insertion position in the specified logical direction.|
|GetPositionAtOffset||Returns a to the position indicated by the specified offset, in symbols, from the beginning of the current and in the specified direction.|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|MemberwiseClone||Creates a shallow copy of the current Object. (Inherited from Object.)|
|ToString||Returns a string that represents the current object. (Inherited from Object.)|
The class is intended to facilitate traversal and manipulation of content in a RichTextBox. The class introduces the following terminology:
Position - A that points to a location in the content of a RichTextBox. A position either occurs between characters in the content, or between content element tags that define structure for the content.
Current Position - The position indicated by the current . Many of the operations that can be performed with a are relative to the current position. Therefore, a position indicated by a is typically referred to as the current position.
Insertion Position - 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 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).
Symbol - For operations, any of the following is considered to be a symbol:
An opening or closing tag for a TextElement element.
A UIElement element contained within an InlineUIContainer. Note that a UIElement is always counted as exactly one symbol. Any additional content or elements contained by the UIElement are not considered symbols.
Each 16-bit Unicode character inside of a text Run element.
Text Container - The element that forms the border for the content. The position indicated by a always occurs within a text container. Currently, a text container can only be a RichTextBox. Typically, operations between instances in different text containers are not supported.
Some of the operations that facilitates include the following:
Performing an ordinal comparison of the current position with a second specified position. For more information, see the CompareTo method.
Translating between positions and symbol offsets into content. For more information, see the GetPositionAtOffset method.
Performing visual hit testing by translating between a position and a Point representing relative coordinates.
Finding a nearby insertion position, or checking whether the current position is an insertion position. For more information, see the GetNextInsertionPosition method and the IsAtInsertionPosition property.
The position and LogicalDirection indicated by a object are immutable. When content is edited or modified, the position indicated by a does not change relative to the surrounding text. Instead, the offset of that position from the beginning of content is adjusted correspondingly to reflect the new relative position in content. For example, a that indicates a position at the beginning of a paragraph continues to point to the beginning of that paragraph even when content is inserted or deleted before or after the paragraph.
The class does not provide any public constructors. An instance of is created by using properties or methods of other objects (including other objects). The following list provides a few examples of methods and properties that create and return a . This list is not exhaustive:
The following code example highlights the first word and underlines the last word in the sentence that you type in the RichTextBox. In this example, a space character is used as the word boundary. The example will also identify if the RichTextBox is empty.
<Canvas x:Name="LayoutRoot" Background="White"> <RichTextBox x:Name="MyRTB1" Width="301" Canvas.Left="0" Canvas.Top="41" /> <Button x:Name="MyButton" Content="Click here" Click="MyButton_Click" Canvas.Left="0" Canvas.Top="71" /> <TextBlock x:Name="MyTBl" Canvas.Left="83" Canvas.Top="71" Height="23" Width="218" /> <TextBlock Canvas.Left="0" Canvas.Top="12" Height="23" Name="textBlock1" Text="Enter a sentence." Width="225" /> </Canvas>
'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 returns true if the RichTextBox is empty. Public Function isRichTextBoxEmpty() As Boolean Dim startPointer As TextPointer = MyRTB1.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward) Dim endPointer As TextPointer = MyRTB1.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward) If (startPointer.CompareTo(endPointer) = 0) Then Return True Else Return False End If End Function 'This method highlights the first word in the RichTextBox Public Sub HighlightFirstWord() Dim StartofContent As TextPointer = MyRTB1.ContentStart Dim currentPointer As TextPointer = StartofContent.GetNextInsertionPosition(LogicalDirection.Forward) If (currentPointer Is Nothing) Then Return End If Dim currentChar As String = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Forward) While ((currentChar <> " ") _ AndAlso (currentChar <> "")) currentPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Forward) currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Forward) End While Dim StartRect As Rect = StartofContent.GetCharacterRect(LogicalDirection.Forward) Dim EndRect As Rect = currentPointer.GetCharacterRect(LogicalDirection.Forward) StartRect.Union(EndRect) CreateHighlightRectangle(StartRect) End Sub Private Function CreateHighlightRectangle(ByVal bounds As Rect) As Rectangle Dim r As Rectangle = New Rectangle r.Fill = New SolidColorBrush(Color.FromArgb(75, 0, 0, 200)) r.Stroke = New SolidColorBrush(Color.FromArgb(230, 0, 0, 254)) r.StrokeThickness = 1 r.Width = bounds.Width r.Height = bounds.Height Canvas.SetLeft(r, bounds.Left) Canvas.SetTop(r, (bounds.Top + 41)) LayoutRoot.Children.Add(r) Return r End Function Private Sub MyButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) If isRichTextBoxEmpty Then MyTBl.Text = "RichTextBox is empty" Else HighlightFirstWord() UnderlineLastWord() MyTBl.Text = "" End If End Sub
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.