{"record":{"id":"c23e9cf1cd0c7b22","repo":"dotnet/wpf","slug":"sr-visual-argumentoutofrange-caretelement","errorCode":null,"errorMessage":"SR.Visual_ArgumentOutOfRange","messagePattern":"SR\\.Visual_ArgumentOutOfRange","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/CaretElement.cs","lineNumber":106,"sourceCode":"\n        #region Protected Methods\n\n        /// <summary>\n        /// Returns the Visual children count.\n        /// </summary>\n        protected override int VisualChildrenCount\n        {\n            get { return (_caretElement == null) ? 0 : 1; }\n        }\n\n        /// <summary>\n        /// Returns the child at the specified index.\n        /// </summary>\n        protected override Visual GetVisualChild(int index)\n        {\n            if (index != 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(index), index, SR.Visual_ArgumentOutOfRange);\n            }\n\n            return _caretElement;\n        }\n\n        // HitTestCore override not to hit testable Caret.\n        protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)\n        {\n            // Return null not to hit testable for CaretElement.\n            return null;\n        }\n\n        // Render override -- we render the selection here.\n        protected override void OnRender(DrawingContext drawingContext)\n        {\n            if (_selectionGeometry != null)\n            {\n                FrameworkElement owner = GetOwnerElement();","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/CaretElement.cs#L88-L124","documentation":"CaretElement.GetVisualChild throws ArgumentOutOfRangeException for any index other than 0 — CaretElement exposes exactly one visual child (its _caretElement). This enforces the Visual/VisualChildrenCount contract for the internal text-system caret.","triggerScenarios":"Enumerating CaretElement's visual children with an index >= 1 or negative; overriding VisualChildrenCount in a subclass inconsistently; tooling walking the visual tree beyond the reported count.","commonSituations":"Custom text-editor controls deriving from or manipulating the caret's visual tree; automation frameworks mis-walking the tree; races where the caret child was swapped between count query and access.","solutions":["Access the child only via VisualTreeHelper.GetChild(caret, 0) / GetChildrenCount, never with hardcoded larger ranges.","Keep index within 0..VisualChildrenCount-1 in any enumeration loop.","In subclasses, override VisualChildrenCount and GetVisualChild consistently.","Restrict visual-tree mutation to the UI thread to avoid count/access races."],"exampleFix":"// before\nfor (int i = 0; i < 2; i++) Visit(caretElement.GetVisualChild(i));\n// after\nint n = VisualTreeHelper.GetChildrenCount(caretElement);\nfor (int i = 0; i < n; i++) Visit(VisualTreeHelper.GetChild(caretElement, i));","handlingStrategy":"validation","validationCode":"int n = VisualTreeHelper.GetChildrenCount(caretElement);\nif (index >= 0 && index < n) { var child = VisualTreeHelper.GetChild(caretElement, index); }","typeGuard":"bool HasVisualChild(Visual parent, int index) => index >= 0 && index < VisualTreeHelper.GetChildrenCount(parent);","tryCatchPattern":"try { var child = VisualTreeHelper.GetChild(caretElement, i); } catch (ArgumentOutOfRangeException) { break; }","preventionTips":["Treat the caret as having exactly one visual child","Enumerate via VisualTreeHelper with live counts","Never hardcode expected child counts for framework internals"],"tags":["wpf","caret","visual-tree","argument-out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}