dotnet/wpf · error · InvalidOperationException

SR.OperationCannotBePerformed

Error message

SR.OperationCannotBePerformed

What it means

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.

Solutions

  1. Truncate the string to the control's limit (query EM_GETLIMITTEXT) before calling SetValue
  2. Split the value across appropriate fields if it doesn't fit
  3. If the app's limit is wrong for your scenario, change EM_SETLIMITTEXT in the target application
  4. Check str length against the limit in the automation script and fail fast with a clear message

Example fix

// before
valuePattern.SetValue(longText); // throws if too long
// after
int limit = SendEM_GETLIMITTEXT(hwnd);
valuePattern.SetValue(longText.Length <= limit ? longText : longText.Substring(0, limit));
Defensive patterns

Strategy: validation

Validate before calling

bool FitsEditLimit(AutomationElement e, string s) { int limit = GetEmLimitText(e); return s.Length <= limit; }

Try / catch

try { valuePattern.SetValue(text); } catch (InvalidOperationException) { text = text.Substring(0, GetEmLimitText(element)); valuePattern.SetValue(text); }

Prevention

When it happens

Trigger: 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).

Common situations: Pasting large payloads into automation, fields intentionally limited by the app (e.g. max 10 characters), or scripts assuming unlimited text capacity.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/f380244627e6728f. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsRichEdit.cs:191

            if (Misc.IsBitSet(WindowStyle, NativeMethods.ES_NUMBER) && !WindowsFormsHelper.IsWindowsFormsControl(_hwnd))
            {
                // check if string contains any non-numeric characters.
                foreach (char ch in str)
                {
                    if (char.IsLetter(ch))
                    {
                        throw new ArgumentException(SR.Format(SR.NotAValidValue, str), "val");
                    }
                }
            }

            // Text/edit box should not enter more characters than what is allowed through keyboard.
            // Determine the max number of chars this editbox accepts

            int result = Misc.ProxySendMessageInt(_hwnd, NativeMethods.EM_GETLIMITTEXT, IntPtr.Zero, IntPtr.Zero);
            if (result < str.Length)
            {
                throw new InvalidOperationException (SR.OperationCannotBePerformed);
            }

            // Send the message...
            result = Misc.ProxySendMessageInt(_hwnd, NativeMethods.WM_SETTEXT, IntPtr.Zero, new StringBuilder(str));
            if (result != 1)
            {
                throw new InvalidOperationException(SR.OperationCannotBePerformed);
            }
        }

        // Request to get the value that this UI element is representing as a string
        string IValueProvider.Value
        {
            get
            {
                return GetValue();
            }
        }

View on GitHub (pinned to 81131a70a4)