{"record":{"id":"02beff99674ad484","repo":"dotnet/wpf","slug":"operation-cannot-be-performed-windowseditboxrange","errorCode":null,"errorMessage":"Operation cannot be performed.","messagePattern":"Operation cannot be performed\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsEditBoxRange.cs","lineNumber":444,"sourceCode":"            {\n                Start = e;\n            }\n            else\n            {\n                End = e;\n            }\n        }\n\n        void ITextRangeProvider.Select()\n        {\n            Misc.SetFocus(_provider._hwnd);\n\n            _provider.SetSel(Start, End);\n        }\n\n        void ITextRangeProvider.AddToSelection()\n        {\n            throw new InvalidOperationException();\n        }\n\n        void ITextRangeProvider.RemoveFromSelection()\n        {\n            throw new InvalidOperationException();\n        }\n\n        void ITextRangeProvider.ScrollIntoView(bool alignToTop)\n        {\n            Misc.SetFocus(_provider._hwnd);\n\n            // Scroll into view is handled differently depending on whether\n            // it is a multi-line control or not.\n            if (_provider.IsMultiline)\n            {\n                int newFirstLine;\n\n                if (alignToTop)","sourceCodeStart":426,"sourceCodeEnd":462,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsEditBoxRange.cs#L426-L462","documentation":"WindowsEditBoxRange throws a bare InvalidOperationException from ITextRangeProvider.AddToSelection (and RemoveFromSelection). A plain Win32 edit control does not support multiple/disjoint selection ranges, so these operations are categorically unsupported.","triggerScenarios":"Calling TextPattern range AddToSelection on any range obtained from a WindowsEditBox TextPattern.","commonSituations":"Generic text-editor automation code that adds a second selection caret; multi-select test scripts reused against single-select edit controls; frameworks that call AddToSelection on all TextRanges uniformly.","solutions":["Use Select() on a single range instead of AddToSelection for edit boxes.","Check whether the control supports multiple selections (rich controls like RICHEDIT) before calling AddToSelection.","Catch InvalidOperationException and fall back to Select().","Drive multi-selection UI through a different control type or an accessibility layer that supports it."],"exampleFix":"// before\n((ITextRangeProvider)range).AddToSelection(); // always throws on edit boxes\n// after\n((ITextRangeProvider)range).Select(); // single-selection edit controls support only Select","handlingStrategy":"fallback","validationCode":"// Win32 edit boxes support a single selection; check control class before AddToSelection\nbool isEditBox = el.Current.ClassName == \"Edit\";","typeGuard":null,"tryCatchPattern":"try { range.AddToSelection(); }\ncatch (InvalidOperationException) { range.Select(); }","preventionTips":["Use Select() for single-selection controls.","Only call AddToSelection on multi-selection-capable controls (RICHEDIT etc.).","Feature-detect capabilities instead of assuming text-editor semantics."],"tags":["uiautomation","textpattern","selection","unsupported"],"backgroundTag":"operation-not-supported","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"}