{"record":{"id":"b38b45e0f9c3fd93","repo":"dotnet/wpf","slug":"sr-rangevaluemax-windowsscrollbar","errorCode":null,"errorMessage":"SR.RangeValueMax","messagePattern":"SR\\.RangeValueMax","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsScrollBar.cs","lineNumber":679,"sourceCode":"            // the richedit is exposing (implicitly through the scrollbar APIs) a max value one higher than the\n            // max value that it will actually allow.\n\n            int max;\n            string classname = Misc.GetClassName(_hwnd);\n            if (classname.ToLower(System.Globalization.CultureInfo.InvariantCulture).Contains(\"richedit\"))\n            {\n                max = si.nMax - si.nPage;\n            }\n            else\n            {\n                max = (si.nMax - si.nPage) + (si.nPage > 0 ? 1 : 0);\n            }\n\n            // Explicit comparisions are made to the MaxPercentage and MinPercentage,\n            // so that we dont miss out the fractions\n            if (val > max )\n            {\n                throw new ArgumentOutOfRangeException(\"value\", val, SR.RangeValueMax);\n            }\n            else if (val < si.nMin)\n            {\n                throw new ArgumentOutOfRangeException(\"value\", val, SR.RangeValueMin);\n            }\n\n            if (_sbFlag == NativeMethods.SB_CTL)\n            {\n                Misc.SetScrollPos(_hwnd, _sbFlag, val, true);\n            }\n            else\n            {\n                // Determine the msg from the style.\n                int msg =\n                    IsScrollBarVertical(_hwnd, _sbFlag) ? NativeMethods.WM_VSCROLL : NativeMethods.WM_HSCROLL;\n\n                // An application is generally programmed to process either the\n                // SB_THUMBTRACK or SB_THUMBPOSITION request code.","sourceCodeStart":661,"sourceCodeEnd":697,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsScrollBar.cs#L661-L697","documentation":"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.","triggerScenarios":"RangeValuePattern.SetValue called with a value greater than the scrollbar's current effective max (nMax - page size + 1), at WindowsScrollBar.cs:679.","commonSituations":"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.","solutions":["Read RangeValue.Current.Maximum fresh immediately before SetValue and clamp the value to it.","Clamp val into [Minimum, Maximum] from the pattern's current properties.","Catch ArgumentOutOfRangeException and retry with a clamped value."],"exampleFix":"// before\nrangeValue.SetValue(100);\n// after\nvar rv = (RangeValuePattern)el.GetCurrentPattern(RangeValuePattern.Pattern);\nrangeValue.SetValue(Math.Min(100, rv.Current.Maximum));","handlingStrategy":"validation","validationCode":"var rv = (RangeValuePattern)element.GetCurrentPattern(RangeValuePattern.Pattern);\nval = Math.Min(val, rv.Current.Maximum);","typeGuard":null,"tryCatchPattern":"try { rv.SetValue(val); }\ncatch (ArgumentOutOfRangeException) { rv.SetValue(rv.Current.Maximum); }","preventionTips":["Read Maximum fresh immediately before SetValue","Never hardcode range endpoints for scroll/track controls","Recompute expected max after content or resize changes"],"tags":["uiautomation","argument-out-of-range","range-value-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"}