dotnet/wpf · error · InvalidOperationException

Start or end specified is past the end of the text range.

Error message

Start or end specified is past the end of the text range.

What it means

ValidateEndpoints verifies that both endpoints of the WindowsEditBoxRange still lie within the provider's text; if Start or End exceeds GetTextLength(), it throws InvalidOperationException with SR.InvalidRangeEndpoint ('Start or end specified is past the end of the text range').

Solutions

  1. Re-acquire the range from the current document state after any text modification.
  2. Before using a cached range, check range endpoints against provider.GetTextLength() and rebuild if out of bounds.
  3. Catch InvalidOperationException and recreate the range via TextPattern.DocumentRange/RangeFromChild.
  4. Synchronize range usage with text-changed events (AutomationPropertyChanged/TextChanged) instead of caching indefinitely.

Example fix

// before
// range captured earlier
range.Select();
// after
if (range == null) range = textPattern.DocumentRange;
try { range.Select(); }
catch (InvalidOperationException) { range = textPattern.DocumentRange; range.Select(); }
Defensive patterns

Strategy: validation

Validate before calling

int len = provider.GetTextLength();
bool endpointsValid = range != null; // UIA ranges expose no direct endpoints; re-validate by use
if (textWasModified) { range = textPattern.DocumentRange; }

Try / catch

try { range.Select(); }
catch (InvalidOperationException) { range = textPattern.DocumentRange; range.Select(); }

Prevention

When it happens

Trigger: Using a range after the underlying edit-box text shrank (text cleared, shortened, or control repurposed) so that cached offsets now exceed the text length.

Common situations: Long-lived automation ranges held across document edits, automated tests typing/clearing text between range operations, or text set asynchronously while a range from the old content is still in use.

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/7f3c562d6cf58909. Report an issue: GitHub.

Appendix: source

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

        // method to set both endpoints simultaneously
        private void MoveTo(int start, int end)
        {
            if (start < 0 || end < start)
            {
                throw new InvalidOperationException(SR.Format(SR.InvalidTextRangeOffset, GetType().FullName));
            }

            _start = start;
            _end = end;
        }

        private void ValidateEndpoints()
        {
            int limit = _provider.GetTextLength();
            if (Start > limit || End > limit)
            {
                throw new InvalidOperationException(SR.Format(SR.InvalidRangeEndpoint, GetType().FullName));
            }
        }

        #endregion Private Methods

        //------------------------------------------------------
        //
        //  Private Properties
        //
        //------------------------------------------------------

        #region Private Properties

        private bool IsDegenerate
        {
            get
            {
                // strictly only needs to be == since never should _start>_end.

View on GitHub (pinned to 81131a70a4)