dotnet/wpf · error · InvalidOperationException

Operation cannot be performed.

Error message

Operation cannot be performed.

What it means

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.

Solutions

  1. Use Select() on a single range instead of AddToSelection for edit boxes.
  2. Check whether the control supports multiple selections (rich controls like RICHEDIT) before calling AddToSelection.
  3. Catch InvalidOperationException and fall back to Select().
  4. Drive multi-selection UI through a different control type or an accessibility layer that supports it.

Example fix

// before
((ITextRangeProvider)range).AddToSelection(); // always throws on edit boxes
// after
((ITextRangeProvider)range).Select(); // single-selection edit controls support only Select
Defensive patterns

Strategy: fallback

Validate before calling

// Win32 edit boxes support a single selection; check control class before AddToSelection
bool isEditBox = el.Current.ClassName == "Edit";

Try / catch

try { range.AddToSelection(); }
catch (InvalidOperationException) { range.Select(); }

Prevention

When it happens

Trigger: Calling TextPattern range AddToSelection on any range obtained from a WindowsEditBox TextPattern.

Common situations: 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.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsEditBoxRange.cs:444

            {
                Start = e;
            }
            else
            {
                End = e;
            }
        }

        void ITextRangeProvider.Select()
        {
            Misc.SetFocus(_provider._hwnd);

            _provider.SetSel(Start, End);
        }

        void ITextRangeProvider.AddToSelection()
        {
            throw new InvalidOperationException();
        }

        void ITextRangeProvider.RemoveFromSelection()
        {
            throw new InvalidOperationException();
        }

        void ITextRangeProvider.ScrollIntoView(bool alignToTop)
        {
            Misc.SetFocus(_provider._hwnd);

            // Scroll into view is handled differently depending on whether
            // it is a multi-line control or not.
            if (_provider.IsMultiline)
            {
                int newFirstLine;

                if (alignToTop)

View on GitHub (pinned to 81131a70a4)