dotnet/wpf · error · InvalidOperationException

SR.ValueReadonly

Error message

SR.ValueReadonly

What it means

WindowsRichEdit's IValueProvider.SetValue refuses to change the text of a rich edit control created with the ES_READONLY window style. Setting a value into a read-only edit box is not permitted, so it throws InvalidOperationException(SR.ValueReadonly), mirroring how a user cannot type into the control.

Solutions

  1. Skip SetValue for read-only fields; if you own the app, remove ES_READONLY or temporarily clear it before automation
  2. Use the target application's own API/IPC to update the data instead of UIA
  3. Check WindowStyle for ES_READONLY before calling SetValue and branch accordingly
  4. If the field should be editable, fix the app to not apply ES_READONLY

Example fix

// before
editBox.GetCurrentPattern(ValuePattern.Pattern).SetValue(text); // throws if ES_READONLY
// after
if ((int)editBoxNative.WindowStyle & ES_READONLY == 0)
    editBox.GetCurrentPattern(ValuePattern.Pattern).SetValue(text);
else
    HandleReadOnlyField();
Defensive patterns

Strategy: validation

Validate before calling

bool CanSetValue(AutomationElement e) { var p = e.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern; return p != null && p.Current.IsReadOnly == false; }

Type guard

bool IsEditable(AutomationElement e) => e.TryGetCurrentPattern(ValuePattern.Pattern, out var p) && !((ValuePattern)p).Current.IsReadOnly;

Try / catch

try { valuePattern.SetValue(text); } catch (InvalidOperationException) { /* field is read-only (ES_READONLY) */ }

Prevention

When it happens

Trigger: Calling UIA ValuePattern.SetValue(string) on a rich edit control whose WindowStyle includes ES_READONLY.

Common situations: Automating forms where the field is intentionally read-only (display-only fields, license text boxes), or applications that flip ES_READONLY on at runtime while automation scripts still try to fill the field.

Related errors


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

Appendix: source

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

            return base.GetElementProperty(idProp);
        }
        #endregion ProxySimple Interface

        #region Value Pattern

        // Sets the text of the edit.
        void IValueProvider.SetValue(string str)
        {
            // Check if the window is disabled
            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
            {
                throw new ElementNotEnabledException();
            }

            if (Misc.IsBitSet(WindowStyle, NativeMethods.ES_READONLY))
            {
                throw new InvalidOperationException(SR.ValueReadonly);
            }

            // check if control only accepts numbers
            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

View on GitHub (pinned to 81131a70a4)