{"record":{"id":"984e069ce0c503aa","repo":"dotnet/wpf","slug":"sr-rangevaluemax","errorCode":null,"errorMessage":"SR.RangeValueMax","messagePattern":"SR\\.RangeValueMax","errorType":"validation","errorClass":"System.ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewScroll.cs","lineNumber":69,"sourceCode":"\n            NativeMethods.ScrollInfo si = new NativeMethods.ScrollInfo\n            {\n                fMask = NativeMethods.SIF_ALL\n            };\n            si.cbSize = Marshal.SizeOf (si.GetType ());\n\n                if (!Misc.GetScrollInfo(_hwnd, _sbFlag, ref si))\n                {\n                    return;\n                }\n\n                int pos = (int)val;\n                // Throw if val is greater than the maximum or less than the minimum.\n                // See remarks for WindowsScrollBar.GetScrollValue(ScrollBarInfo.MaximumPosition)\n                // regarding this calculation of the allowed maximum.\n                if (pos > si.nMax - si.nPage + (si.nPage > 0 ? 1 : 0))\n                {\n                    throw new ArgumentOutOfRangeException(\"value\", val, SR.RangeValueMax);\n                }\n                else if (pos < si.nMin)\n                {\n                    throw new ArgumentOutOfRangeException(\"value\", val, SR.RangeValueMin);\n                }\n\n                // LVM_SCROLL does not work in mode Report, use SetScrollPos instead\n                bool isVerticalScroll = IsScrollBarVertical(_hwnd, _sbFlag);\n                if (isVerticalScroll && WindowsListView.InReportView (_hwnd))\n                {\n                    Misc.SetScrollPos(_hwnd, _sbFlag, pos, true);\n                    return;\n                }\n\n                // get the \"full size\" of the list-view\n                int size = WindowsListView.ApproximateViewRect (_hwnd);\n\n                // delta between current and user-requested position in pixels","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewScroll.cs#L51-L87","documentation":"IRangeValueProvider.SetValue on a list-view scroll bar throws ArgumentOutOfRangeException with SR.RangeValueMax when the requested position exceeds the scroll bar's effective maximum. Because of Win32 scroll-bar semantics, the allowed maximum is nMax - nPage + (nPage > 0 ? 1 : 0), not simply nMax. The provider fetches SCROLLINFO (SIF_ALL) and validates pos against this computed maximum before scrolling.","triggerScenarios":"Calling RangeValuePattern.SetValue(val) on a list-view scroll bar with val greater than si.nMax - si.nPage + (si.nPage > 0 ? 1 : 0), where si comes from GetScrollInfo with SIF_ALL. Failing to account for the nPage adjustment when reading Maximum is the typical cause.","commonSituations":"Automation scripts computing the max scroll position from the RangeValue.Maximum property or raw nMax and passing values at/above it without the page-size correction; list view resized so nPage changed between reading the range and setting the value.","solutions":["Clamp the requested value to the effective maximum: min(val, nMax - nPage + (nPage > 0 ? 1 : 0))","Query the current scroll info (or RangeValue.Maximum) immediately before SetValue, since nPage/nMax change with resizing","Catch ArgumentOutOfRangeException and clamp/retry within the valid range","Use Scroll patterns or SendMessage(WM_HSCROLL/WM_VSCROLL) with SB_BOTTOM/SB_PAGEDOWN instead of absolute positions"],"exampleFix":"// before\nrangeValue.SetValue(maximum); // Maximum can exceed the scrollable maximum\n\n// after\ndouble effectiveMax = rangeValue.Current.Maximum - rangeValue.Current.LargeChange + 1;\nrangeValue.SetValue(Math.Min(value, Math.Max(rangeValue.Current.Minimum, effectiveMax)));","handlingStrategy":"validation","validationCode":"// C#: clamp to the effective scrollable maximum before calling SetValue\nvar rv = (RangeValuePattern)scrollBar.GetCurrentPattern(RangeValuePattern.Pattern);\nvar info = rv.Current;\n// Win32 effective max accounts for page size (LargeChange maps to nPage)\ndouble effectiveMax = info.Maximum - info.LargeChange + 1;\nvalue = Math.Max(info.Minimum, Math.Min(value, Math.Max(info.Minimum, effectiveMax)));\nrv.SetValue(value);","typeGuard":"bool InEffectiveRange(RangeValuePattern.RangeValueInformation info, double v) =>\n    v >= info.Minimum && v <= Math.Max(info.Minimum, info.Maximum - info.LargeChange + 1);","tryCatchPattern":"try\n{\n    rv.SetValue(value);\n}\ncatch (ArgumentOutOfRangeException)\n{\n    var i = rv.Current;\n    rv.SetValue(Math.Min(value, Math.Max(i.Minimum, i.Maximum - i.LargeChange + 1)));\n}","preventionTips":["Never assume Maximum equals the scrollable position; account for page size (nPage)","Query scroll info immediately before setting values","Clamp all computed positions into the effective range","Prefer relative scroll actions (PageDown/Bottom) over absolute positions when possible"],"tags":["uiautomation","rangevaluepattern","argumentoutofrange","scrollbar"],"backgroundTag":"value-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}