{"record":{"id":"f380244627e6728f","repo":"dotnet/wpf","slug":"sr-operationcannotbeperformed-windowsrichedit","errorCode":null,"errorMessage":"SR.OperationCannotBePerformed","messagePattern":"SR\\.OperationCannotBePerformed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsRichEdit.cs","lineNumber":191,"sourceCode":"            if (Misc.IsBitSet(WindowStyle, NativeMethods.ES_NUMBER) && !WindowsFormsHelper.IsWindowsFormsControl(_hwnd))\n            {\n                // check if string contains any non-numeric characters.\n                foreach (char ch in str)\n                {\n                    if (char.IsLetter(ch))\n                    {\n                        throw new ArgumentException(SR.Format(SR.NotAValidValue, str), \"val\");\n                    }\n                }\n            }\n\n            // Text/edit box should not enter more characters than what is allowed through keyboard.\n            // Determine the max number of chars this editbox accepts\n\n            int result = Misc.ProxySendMessageInt(_hwnd, NativeMethods.EM_GETLIMITTEXT, IntPtr.Zero, IntPtr.Zero);\n            if (result < str.Length)\n            {\n                throw new InvalidOperationException (SR.OperationCannotBePerformed);\n            }\n\n            // Send the message...\n            result = Misc.ProxySendMessageInt(_hwnd, NativeMethods.WM_SETTEXT, IntPtr.Zero, new StringBuilder(str));\n            if (result != 1)\n            {\n                throw new InvalidOperationException(SR.OperationCannotBePerformed);\n            }\n        }\n\n        // Request to get the value that this UI element is representing as a string\n        string IValueProvider.Value\n        {\n            get\n            {\n                return GetValue();\n            }\n        }","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsRichEdit.cs#L173-L209","documentation":"Before sending WM_SETTEXT, SetValue queries the control's text limit via EM_GETLIMITTEXT. If the proposed string is longer than that limit, the control could not accept it via keyboard input either, so SetValue throws InvalidOperationException(SR.OperationCannotBePerformed) instead of silently truncating.","triggerScenarios":"Calling ValuePattern.SetValue(str) where str.Length exceeds the rich edit control's EM_GETLIMITTEXT limit (set by EM_SETLIMITTEXT, often a default like 32767 or an app-chosen smaller cap).","commonSituations":"Pasting large payloads into automation, fields intentionally limited by the app (e.g. max 10 characters), or scripts assuming unlimited text capacity.","solutions":["Truncate the string to the control's limit (query EM_GETLIMITTEXT) before calling SetValue","Split the value across appropriate fields if it doesn't fit","If the app's limit is wrong for your scenario, change EM_SETLIMITTEXT in the target application","Check str length against the limit in the automation script and fail fast with a clear message"],"exampleFix":"// before\nvaluePattern.SetValue(longText); // throws if too long\n// after\nint limit = SendEM_GETLIMITTEXT(hwnd);\nvaluePattern.SetValue(longText.Length <= limit ? longText : longText.Substring(0, limit));","handlingStrategy":"validation","validationCode":"bool FitsEditLimit(AutomationElement e, string s) { int limit = GetEmLimitText(e); return s.Length <= limit; }","typeGuard":null,"tryCatchPattern":"try { valuePattern.SetValue(text); } catch (InvalidOperationException) { text = text.Substring(0, GetEmLimitText(element)); valuePattern.SetValue(text); }","preventionTips":["Query the field's max length (EM_GETLIMITTEXT / GetWindowTextLength limit) before writing","Truncate long payloads to the control's documented limit","Design automation data fixtures within the app's field length constraints"],"tags":["uiautomation","richedit","text-limit","length-exceeded"],"backgroundTag":"value-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"}