{"record":{"id":"ba5b780145a0e1ff","repo":"dotnet/wpf","slug":"sr-textprovider-textselectionnotsupported-textrangeadaptor","errorCode":null,"errorMessage":"SR.TextProvider_TextSelectionNotSupported","messagePattern":"SR\\.TextProvider_TextSelectionNotSupported","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextRangeAdaptor.cs","lineNumber":2036,"sourceCode":"                _end = targetPointer.CreatePointer();\n                // Ensure the correct ordering of the endpoint.\n                if (_start.CompareTo(_end) > 0)\n                {\n                    _start = _end.CreatePointer();\n                }\n            }\n        }\n\n        /// <summary>\n        /// Selects the text of the range within the provider.  If the provider does not have a concept of selection then\n        /// it should return false for ITextProvider.SupportsTextSelection property and throw an InvalidOperation \n        /// exception for this method.\n        /// </summary>\n        void ITextRangeProvider.Select()\n        {\n            if (((ITextProvider)_textAdaptor).SupportedTextSelection == SupportedTextSelection.None)\n            {\n                throw new InvalidOperationException(SR.TextProvider_TextSelectionNotSupported);\n            }\n\n            Normalize();\n\n            _textAdaptor.Select(_start, _end);\n        }\n\n        /// <summary>\n        /// Adds the text of the range to the current selection.  If the provider does not have a concept of selection\n        /// or does not support multiple disjoint selection then it throw an InvalidOperation \n        /// exception for this method.\n        /// </summary>\n        void ITextRangeProvider.AddToSelection()\n        {\n            throw new InvalidOperationException();\n        }\n\n        /// <summary>","sourceCodeStart":2018,"sourceCodeEnd":2054,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextRangeAdaptor.cs#L2018-L2054","documentation":"ITextRangeProvider.Select() on TextRangeAdaptor throws InvalidOperationException (SR.TextProvider_TextSelectionNotSupported) when the underlying control's text provider reports SupportedTextSelection.None, i.e. the control does not support text selection at all. Calling Select on such a range is an invalid operation for that control, not a bad argument.","triggerScenarios":"Calling ITextRangeProvider.Select() (via the Selection pattern, e.g. TextPattern.GetSelection or RangeFromChild ranges) on a WPF text element whose automation peer exposes TextPattern but not selectable text (e.g. TextBlock, or read-only flow content without selection support).","commonSituations":"Automation scripts that select text generically across controls and encounter non-selectable elements like TextBlock or styled read-only content; tests written against TextBox behavior reused on TextBlock; control restyling changing which peer/provider is used.","solutions":["Check ((ITextProvider)provider).SupportedTextSelection != SupportedTextSelection.None before calling Select().","Use TextPattern.GetSelection() only on controls whose peers advertise SupportedTextSelection.Single/Multiple.","Wrap Select() in try/catch for InvalidOperationException and skip selection for non-selectable controls."],"exampleFix":"// before\nrange.Select();\n\n// after\nif (textProvider.SupportedTextSelection != SupportedTextSelection.None)\n{\n    range.Select();\n}","handlingStrategy":"type-guard","validationCode":"if (((ITextProvider)textPatternProvider).SupportedTextSelection == SupportedTextSelection.None)\n    return; // control cannot select text\nrange.Select();","typeGuard":"static bool SupportsSelection(ITextProvider p) => p.SupportedTextSelection != SupportedTextSelection.None;","tryCatchPattern":"try\n{\n    range.Select();\n}\ncatch (InvalidOperationException)\n{\n    // control does not support text selection; skip or fall back\n}","preventionTips":["Check SupportedTextSelection before any programmatic selection via TextPattern ranges.","Do not assume TextBox-like behavior for TextBlock or other read-only text elements.","Feature-detect automation patterns rather than hard-coding per-control assumptions."],"tags":["wpf","ui-automation","unsupported-operation","text-selection"],"backgroundTag":"unsupported-operation","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"}