dotnet/wpf · error · ArgumentException
SR.RichTextBox_PointerNotInSameDocument
Error message
SR.RichTextBox_PointerNotInSameDocument
What it means
RichTextBox.CaretPosition assigns the caret to a TextPointer, and the pointer must belong to the same text container (document) as the selection. If Selection.Start.IsInSameDocument(value) is false the setter throws ArgumentException(SR.RichTextBox_PointerNotInSameDocument), preventing cross-document TextPointer use.
Solutions
- Obtain the TextPointer from the same RichTextBox's Document/Selection before assigning CaretPosition.
- After replacing the Document, recompute the pointer from the new document instead of reusing the old one.
- Convert positions to an offset (e.g. via TextPointer offsets) and re-create the pointer against the target document.
Example fix
// before rtb.CaretPosition = pointerFromOldDocument; // after var newPointer = rtb.Document.ContentStart.GetPositionAtOffset(savedOffset); rtb.CaretPosition = newPointer;
Defensive patterns
Strategy: type-guard
Validate before calling
if (pointer == null || !richTextBox.Selection.Start.IsInSameDocument(pointer))
pointer = richTextBox.Document.ContentStart.GetPositionAtOffset(savedOffset) ?? richTextBox.Document.ContentStart;
richTextBox.CaretPosition = pointer; Type guard
bool IsPointerInDocument(RichTextBox rtb, TextPointer p) =>
p != null && rtb.Selection.Start.IsInSameDocument(p); Try / catch
try { richTextBox.CaretPosition = pointer; }
catch (ArgumentException) { richTextBox.CaretPosition = richTextBox.Document.ContentStart; } Prevention
- Recompute TextPointers from the current Document after any document swap.
- Never cache TextPointers across document lifetime boundaries; store offsets instead.
- Only pass pointers obtained from the same control's Document/Selection.
When it happens
Trigger: Assigning CaretPosition with a TextPointer obtained from a different FlowDocument or another RichTextBox — e.g. caching pointers across document swaps, or setting the caret from a pointer captured before Document was replaced.
Common situations: Restoring saved caret positions after loading new content; copying positions between two RichTextBox instances; using pointers from a FlowDocumentScrollViewer or TextPointer from a stale document instance.
Understand the failure class
Background: "must be a positive integer", "cannot be empty", "invalid argument": how invalid-argument errors work across open-source libraries — this error's family across 33 libraries.
Related errors
- SR.CanOnlyHaveOneChild
- SR.RichTextBox_DocumentBelongsToAnotherRichTextBoxAlready
- SR.UnexpectedParameterType
- ArgumentNullException(paramName)
- ArgumentOutOfRangeException (timeout was Duration.Automatic)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/9bf6c82429fab68b.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RichTextBox.cs:576
}
/// <summary>
/// Position of the caret.
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public TextPointer CaretPosition
{
get
{
return Selection.MovingPosition;
}
set
{
ArgumentNullException.ThrowIfNull(value);
if (!Selection.Start.IsInSameDocument(value))
{
throw new ArgumentException(SR.RichTextBox_PointerNotInSameDocument, nameof(value));
}
Selection.SetCaretToPosition(value, value.LogicalDirection, /*allowStopAtLineEnd:*/true, /*allowStopNearSpace:*/false);
}
}
#endregion Content Accessing Properties
#endregion Public Properties
//------------------------------------------------------
//
// Internal Methods
//
//------------------------------------------------------
#region Internal Methods
// Returns the DependencyObjectType for the registered ThemeStyleKey's default View on GitHub (pinned to 81131a70a4)