{"record":{"id":"e24d3c14cf1596cf","repo":"dotnet/wpf","slug":"throw-new-argumentoutofrangeexception-ishorizontal-hwnd","errorCode":null,"errorMessage":"throw new ArgumentOutOfRangeException(IsHorizontal(_hwnd) ? \"horizontalPercent\" : \"verticalPercent\", SR.ScrollBarOutOfRange);","messagePattern":"throw new ArgumentOutOfRangeException\\(IsHorizontal\\(_hwnd\\) \\? \"horizontalPercent\" : \"verticalPercent\", SR\\.ScrollBarOutOfRange\\);","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/windowspager.cs","lineNumber":421,"sourceCode":"        #region Private Methods\n\n        private bool IsScrollable()\n        {\n            return IsVisible(_hwnd, NativeMethods.PGB_BOTTOMORRIGHT) || IsVisible(_hwnd, NativeMethods.PGB_TOPORLEFT);\n        }\n\n        // Scrolls the pager by a given percent from its current position.\n        private bool ScrollByPercent(double scrollPercent)\n        {\n            // Check params\n            if ((int)scrollPercent == (int)ScrollPattern.NoScroll)\n            {\n                return true;\n            }\n\n            if (scrollPercent < 0 || scrollPercent > 100)\n            {\n                throw new ArgumentOutOfRangeException(IsHorizontal(_hwnd) ? \"horizontalPercent\" : \"verticalPercent\", SR.ScrollBarOutOfRange);\n            }\n\n            NativeMethods.Win32Rect rcChild = new NativeMethods.Win32Rect();\n            int cRange = ScrollRange (ref rcChild);\n\n            // indicative of an error\n            if (cRange < 0)\n            {\n                return false;\n            }\n\n            // Do proper rounding\n            int newPos = (int) (cRange * scrollPercent / 100 + 0.5);\n\n            // Sometimes the PGM_SETPOS fails. Try 3 times when this happens\n            for (int i = 0; i < 3; i++)\n            {\n                Misc.ProxySendMessage(_hwnd, NativeMethods.PGM_SETPOS, IntPtr.Zero, new IntPtr(newPos));","sourceCodeStart":403,"sourceCodeEnd":439,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/windowspager.cs#L403-L439","documentation":"The ScrollByPercent helper (called from SetScrollPercent) validates the percent and throws ArgumentOutOfRangeException naming horizontalPercent or verticalPercent (chosen by pager orientation) when the value is outside 0..100. It runs after the NoScroll sentinel check, so only concrete percents are range-checked.","triggerScenarios":"Calling SetScrollPercent with a value < 0 or > 100 on the applicable axis, e.g. 150 or -5 (other than -1/NoScroll which is allowed).","commonSituations":"Computing a percent from an arithmetic formula (ratio scaling, DPI math) that overflows the 0-100 range; passing percent/100 fractions by mistake.","solutions":["Clamp the value into [0, 100] (Math.Clamp) before calling SetScrollPercent","Keep NoScroll (-1) as the only out-of-range sentinel accepted","Compute percents as Math.Round(100.0 * position / range) and assert the range"],"exampleFix":"// before\nscrollPattern.SetScrollPercent(150, ScrollPattern.NoScroll);\n// after\ndouble pct = Math.Clamp(computedPercent, 0, 100);\nscrollPattern.SetScrollPercent(pct, ScrollPattern.NoScroll);","handlingStrategy":"validation","validationCode":"if (pct != ScrollPattern.NoScroll && (pct < 0 || pct > 100))\n    throw new ArgumentException($\"percent {pct} outside 0..100\", nameof(pct));","typeGuard":"static bool IsValidScrollPercent(double p) => p == ScrollPattern.NoScroll || (p >= 0 && p <= 100);","tryCatchPattern":"try { scrollPattern.SetScrollPercent(pct, ScrollPattern.NoScroll); }\ncatch (ArgumentOutOfRangeException) { pct = Math.Clamp(pct, 0, 100); scrollPattern.SetScrollPercent(pct, ScrollPattern.NoScroll); }","preventionTips":["Clamp computed percents with Math.Clamp(0, 100) before calling","Remember NoScroll (-1) is the only permitted out-of-range sentinel","Watch arithmetic that scales percents (ratios, DPI factors) for overflow"],"tags":["uiautomation","argument-out-of-range","scroll-pattern","wpf"],"backgroundTag":"argument-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}