dotnet/wpf · error · ArgumentOutOfRangeException

SR.RangeValueMax

Error message

SR.RangeValueMax

What it means

WindowsScrollBar.SetScrollValue compares the requested value against the scrollbar's computed maximum; val > max throws ArgumentOutOfRangeException with SR.RangeValueMax. The max is derived from GetScrollInfo (nMin/nMax and page size), not the pattern's cached Maximum, so it can differ at runtime.

Solutions

  1. Read RangeValue.Current.Maximum fresh immediately before SetValue and clamp the value to it.
  2. Clamp val into [Minimum, Maximum] from the pattern's current properties.
  3. Catch ArgumentOutOfRangeException and retry with a clamped value.

Example fix

// before
rangeValue.SetValue(100);
// after
var rv = (RangeValuePattern)el.GetCurrentPattern(RangeValuePattern.Pattern);
rangeValue.SetValue(Math.Min(100, rv.Current.Maximum));
Defensive patterns

Strategy: validation

Validate before calling

var rv = (RangeValuePattern)element.GetCurrentPattern(RangeValuePattern.Pattern);
val = Math.Min(val, rv.Current.Maximum);

Try / catch

try { rv.SetValue(val); }
catch (ArgumentOutOfRangeException) { rv.SetValue(rv.Current.Maximum); }

Prevention

When it happens

Trigger: RangeValuePattern.SetValue called with a value greater than the scrollbar's current effective max (nMax - page size + 1), at WindowsScrollBar.cs:679.

Common situations: Caching RangeValue.Maximum and the content shrinking before SetValue; hardcoding values larger than the control's content; window resized so fewer pages fit, reducing effective max.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsScrollBar.cs:679

            // the richedit is exposing (implicitly through the scrollbar APIs) a max value one higher than the
            // max value that it will actually allow.

            int max;
            string classname = Misc.GetClassName(_hwnd);
            if (classname.ToLower(System.Globalization.CultureInfo.InvariantCulture).Contains("richedit"))
            {
                max = si.nMax - si.nPage;
            }
            else
            {
                max = (si.nMax - si.nPage) + (si.nPage > 0 ? 1 : 0);
            }

            // Explicit comparisions are made to the MaxPercentage and MinPercentage,
            // so that we dont miss out the fractions
            if (val > max )
            {
                throw new ArgumentOutOfRangeException("value", val, SR.RangeValueMax);
            }
            else if (val < si.nMin)
            {
                throw new ArgumentOutOfRangeException("value", val, SR.RangeValueMin);
            }

            if (_sbFlag == NativeMethods.SB_CTL)
            {
                Misc.SetScrollPos(_hwnd, _sbFlag, val, true);
            }
            else
            {
                // Determine the msg from the style.
                int msg =
                    IsScrollBarVertical(_hwnd, _sbFlag) ? NativeMethods.WM_VSCROLL : NativeMethods.WM_HSCROLL;

                // An application is generally programmed to process either the
                // SB_THUMBTRACK or SB_THUMBPOSITION request code.

View on GitHub (pinned to 81131a70a4)