TextPointer.GetCharacterRect Method

Returns a bounding box for content that borders the current TextPointer in the specified logical direction.

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

public Rect GetCharacterRect(
	LogicalDirection direction


Type: System.Windows.Documents.LogicalDirection
One of the LogicalDirection values that specify the logical direction in which to find a content bounding box.

Return Value

Type: System.Windows.Rect
A Rect for content that borders the current TextPointer in the specified direction, or Rect.Empty if current and valid layout information is unavailable.

The return value is always the edge of the next Unicode content, that is, the element edges are invisible. If there is no content in the indicated direction, the return value is the Rect of the content in the opposite direction. If the RichTextBox is empty, the return value is a zero-width Rect with the default line height.

The following code uses the GetCharacterRect method to highlight the first word in the RichTextBox. In this example, a space character is used as the word boundary. This code example is part of a larger example used in the TextPointer class.

Run this sample

//This method highlights the first word in the RichTextBox
public void HighlightFirstWord()
    TextPointer StartofContent = MyRTB1.ContentStart;
    TextPointer currentPointer = StartofContent.GetNextInsertionPosition(LogicalDirection.Forward);

    if (currentPointer == null)

    string currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Forward);

    while (currentChar != " " && currentChar != "")
        currentPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Forward);
        currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Forward);

    Rect StartRect = StartofContent.GetCharacterRect(LogicalDirection.Forward);
    Rect EndRect = currentPointer.GetCharacterRect(LogicalDirection.Forward);


private Rectangle CreateHighlightRectangle(Rect bounds)
    Rectangle r = 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);


    return r;


Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions