{"record":{"id":"9bf6c82429fab68b","repo":"dotnet/wpf","slug":"sr-richtextbox-pointernotinsamedocument","errorCode":null,"errorMessage":"SR.RichTextBox_PointerNotInSameDocument","messagePattern":"SR\\.RichTextBox_PointerNotInSameDocument","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RichTextBox.cs","lineNumber":576,"sourceCode":"        }\n\n        /// <summary>\n        /// Position of the caret.\n        /// </summary>\n        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\n        public TextPointer CaretPosition\n        {\n            get\n            {\n                return Selection.MovingPosition;\n            }\n\n            set\n            {\n                ArgumentNullException.ThrowIfNull(value);\n                if (!Selection.Start.IsInSameDocument(value))\n                {\n                    throw new ArgumentException(SR.RichTextBox_PointerNotInSameDocument, nameof(value));\n                }\n                Selection.SetCaretToPosition(value, value.LogicalDirection, /*allowStopAtLineEnd:*/true, /*allowStopNearSpace:*/false);\n            }\n        }\n        \n        #endregion Content Accessing Properties\n\n        #endregion Public Properties\n\n        //------------------------------------------------------\n        //\n        //  Internal Methods\n        //\n        //------------------------------------------------------\n\n        #region Internal Methods\n\n        // Returns the DependencyObjectType for the registered ThemeStyleKey's default ","sourceCodeStart":558,"sourceCodeEnd":594,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/RichTextBox.cs#L558-L594","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nrtb.CaretPosition = pointerFromOldDocument;\n// after\nvar newPointer = rtb.Document.ContentStart.GetPositionAtOffset(savedOffset);\nrtb.CaretPosition = newPointer;","handlingStrategy":"type-guard","validationCode":"if (pointer == null || !richTextBox.Selection.Start.IsInSameDocument(pointer))\n    pointer = richTextBox.Document.ContentStart.GetPositionAtOffset(savedOffset) ?? richTextBox.Document.ContentStart;\nrichTextBox.CaretPosition = pointer;","typeGuard":"bool IsPointerInDocument(RichTextBox rtb, TextPointer p) =>\n    p != null && rtb.Selection.Start.IsInSameDocument(p);","tryCatchPattern":"try { richTextBox.CaretPosition = pointer; }\ncatch (ArgumentException) { richTextBox.CaretPosition = richTextBox.Document.ContentStart; }","preventionTips":["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."],"tags":["wpf","richtextbox","textpointer","argument-exception"],"backgroundTag":"invalid-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}