dotnet/wpf · error · ArgumentOutOfRangeException

SR.ScrollBarOutOfRange

Error message

SR.ScrollBarOutOfRange

What it means

The per-axis helper inside WindowsScroll.SetScrollPercent validates the requested percent before issuing the Win32 scroll: values below 0 or above 100 throw ArgumentOutOfRangeException with SR.ScrollBarOutOfRange. The parameter name reported depends on which bar (horizontalPercent or verticalPercent) is out of range.

Solutions

  1. Clamp the computed percentage to the 0-100 range before calling SetScrollPercent.
  2. Use ScrollPattern.NoScroll (-1), not 0 or other negatives, to mean 'do not scroll this axis'.
  3. Catch ArgumentOutOfRangeException and clamp/retry with a valid value.

Example fix

// before
scrollPattern.SetScrollPercent(fraction, ScrollPattern.NoScroll); // fraction = 0.75
// after
scrollPattern.SetScrollPercent(Math.Max(0, Math.Min(100, fraction * 100)), ScrollPattern.NoScroll);
Defensive patterns

Strategy: validation

Validate before calling

double pct = Math.Max(0, Math.Min(100, computedPercent));
sp.SetScrollPercent(pct, ScrollPattern.NoScroll);

Try / catch

try { sp.SetScrollPercent(pct, ScrollPattern.NoScroll); }
catch (ArgumentOutOfRangeException) { sp.SetScrollPercent(Math.Clamp(pct, 0, 100), ScrollPattern.NoScroll); }

Prevention

When it happens

Trigger: ScrollPattern.SetScrollPercent called with a value < 0 or > 100 (and not the NoScroll sentinel) for the horizontal or vertical axis, caught at WindowsScroll.cs:283.

Common situations: Computing a percent from element bounds and producing e.g. 150 or -5; forgetting that -1 is the special NoScroll sentinel and passing -2; unit confusion (0.0-1.0 fraction passed instead of 0-100 percent).

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsScroll.cs:283

        // Request to scroll a control horizontally or vertically by a specified amount.
        private static bool SetScrollPercent(IntPtr hwnd, double fScrollPos, int sbFlag, out bool forceResults)
        {
            forceResults = false;
            // Check param
            if ((int)fScrollPos == (int)ScrollPattern.NoScroll)
            {
                return true;
            }

            if (!Scrollable(hwnd, sbFlag))
            {
                return false;
            }

            if (fScrollPos < 0 || fScrollPos > 100)
            {
                throw new ArgumentOutOfRangeException(sbFlag == NativeMethods.SB_HORZ ? "horizontalPercent" : "verticalPercent", SR.ScrollBarOutOfRange);
            }

            // Get Max & min                    
            NativeMethods.ScrollInfo si = new NativeMethods.ScrollInfo
            {
                fMask = NativeMethods.SIF_ALL
            };
            si.cbSize = Marshal.SizeOf(si.GetType ());

            // if no scroll bar return false
            // on Win 6.0 success is false
            // on other system check through the scroll info is a scroll bar is there
            if (!Misc.GetScrollInfo(hwnd, sbFlag, ref si) ||
                !((si.nMax != si.nMin && si.nPage != si.nMax - si.nMin + 1)))
            {
                return false;
            }

View on GitHub (pinned to 81131a70a4)