{"record":{"id":"f24f7b336ef0bc02","repo":"dotnet/wpf","slug":"elementnotenabledexception-windowslistviewscroll","errorCode":null,"errorMessage":"ElementNotEnabledException","messagePattern":"ElementNotEnabledException","errorType":"exception","errorClass":"System.Windows.Automation.ElementNotEnabledException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewScroll.cs","lineNumber":49,"sourceCode":"            internal WindowsListViewScrollBar(IntPtr hwnd, ProxyFragment parent, int item, int sbFlag)\n                : base( hwnd, parent, item, sbFlag){}\n\n            #endregion Constructors\n\n            //------------------------------------------------------\n            //\n            //  Patterns Implementation\n            //\n            //------------------------------------------------------\n\n            #region RangeValue Pattern\n\n            void IRangeValueProvider.SetValue(double val)\n            {\n                // Check if the window is disabled\n                if (!SafeNativeMethods.IsWindowEnabled (_hwnd))\n                {\n                    throw new ElementNotEnabledException();\n                }\n\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))","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewScroll.cs#L31-L67","documentation":"IRangeValueProvider.SetValue on a list-view scroll bar throws ElementNotEnabledException when the scroll bar's HWND is disabled. The UIA RangeValue contract requires that setting a value on a disabled element fails with this exception. The provider verifies SafeNativeMethods.IsWindowEnabled(_hwnd) before attempting to change the scroll position.","triggerScenarios":"Calling RangeValuePattern.SetValue() on a WindowsListViewScroll provider whose scroll bar window is disabled (WS_DISABLED, or disabled via a disabled parent window so IsWindowEnabled returns false).","commonSituations":"See trigger scenarios.","solutions":["Check the element's IsEnabled property before calling SetValue and defer the call until it is true","Re-enable the ListView/window in the application before programmatic scrolling","Retry the SetValue after the disabling condition (modal state, busy operation) is gone","Catch ElementNotEnabledException and treat the scroll operation as currently unavailable"],"exampleFix":"// before\n((RangeValuePattern)scrollBar.GetCurrentPattern(RangeValuePattern.Pattern)).SetValue(50);\n\n// after\nvar pattern = (RangeValuePattern)scrollBar.GetCurrentPattern(RangeValuePattern.Pattern);\nif (scrollBar.Current.IsEnabled)\n{\n    pattern.SetValue(50);\n}","handlingStrategy":"validation","validationCode":"// C#: verify enabled state and range before SetValue\nvar rv = (RangeValuePattern)scrollBar.GetCurrentPattern(RangeValuePattern.Pattern);\nif (!scrollBar.Current.IsEnabled)\n    throw new SkipRetryException(\"Scroll bar is disabled\");\nif (value < rv.Current.Minimum || value > rv.Current.Maximum)\n    throw new ArgumentOutOfRangeException(nameof(value));\nrv.SetValue(value);","typeGuard":"bool IsScrollable(AutomationElement el) =>\n    el.Current.IsEnabled && el.TryGetCurrentPattern(RangeValuePattern.Pattern, out _);","tryCatchPattern":"try\n{\n    ((RangeValuePattern)el.GetCurrentPattern(RangeValuePattern.Pattern)).SetValue(val);\n}\ncatch (ElementNotEnabledException)\n{\n    // retry after the control is re-enabled\ncatch (ArgumentOutOfRangeException)\n{\n    // clamp to Minimum/Maximum and retry\n}","preventionTips":["Check IsEnabled before every SetValue call","Re-read RangeValue.Minimum/Maximum immediately before setting (they change on resize)","Avoid caching scroll ranges across UI updates","Use pattern availability checks (TryGetCurrentPattern) before casting patterns"],"tags":["uiautomation","rangevaluepattern","disabled-control","scrollbar"],"backgroundTag":"invalid-state-transition","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"}