{"record":{"id":"26118d988ec5ee35","repo":"dotnet/wpf","slug":"operation-cannot-be-performed-windowseditbox","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/WindowsEditBox.cs","lineNumber":293,"sourceCode":"            if (Misc.IsBitSet(styles, NativeMethods.ES_NUMBER))\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            int result = Misc.ProxySendMessageInt(_hwnd, NativeMethods.EM_GETLIMITTEXT, IntPtr.Zero, IntPtr.Zero);\n            // A result of -1 means that no limit is set.\n            if (result != -1 && 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":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsEditBox.cs#L275-L311","documentation":"The Win32 edit box proxy throws InvalidOperationException(OperationCannotBePerformed) from IValueProvider.SetValue when the requested text is longer than the control's text limit. It queries EM_GETLIMITTEXT; if a limit is set (result != -1) and the string exceeds it, the operation is rejected before sending WM_SETTEXT.","triggerScenarios":"Calling IValueProvider.SetValue with a string whose length exceeds the value returned by EM_GETLIMITTEXT for the edit control (limit set via EM_SETLIMITTEXT or the control default, e.g. 32766 chars).","commonSituations":"Pasting large log text or multiline blobs into a short input field; a test framework filling a max-length-limited field with generated data; migrating scripts between controls with different limits.","solutions":["Truncate the string to the control's limit before calling SetValue.","Raise the limit in the target application via EM_SETLIMITTEXT.","Use a multiline/large control (or write to the data source directly) for long content.","Catch InvalidOperationException and split input into allowed-size chunks if the control semantics permit."],"exampleFix":"// before\nvaluePattern.SetValue(longText); // throws if longText.Length > limit\n// after\nint limit = /* EM_GETLIMITTEXT result, or int.MaxValue if -1 */;\nvaluePattern.SetValue(longText.Length > limit ? longText.Substring(0, limit) : longText);","handlingStrategy":"validation","validationCode":"int limit = SendMessage(hwnd, EM_GETLIMITTEXT, 0, 0);\nif (limit != -1 && text.Length > limit)\n    text = text.Substring(0, limit); // or reject","typeGuard":null,"tryCatchPattern":"try { vp.SetValue(text); }\ncatch (InvalidOperationException) { /* text exceeds EM limit; truncate or split */ }","preventionTips":["Query EM_GETLIMITTEXT before writing.","Keep generated test data within control limits.","Raise EM_SETLIMITTEXT in the app when longer content is required."],"tags":["uiautomation","winforms","editbox","length-limit"],"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"}