{"record":{"id":"6e724093be815b34","repo":"dotnet/wpf","slug":"sr-operationcannotbeperformed-windowstoolbar","errorCode":null,"errorMessage":"SR.OperationCannotBePerformed","messagePattern":"SR\\.OperationCannotBePerformed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsToolbar.cs","lineNumber":652,"sourceCode":"        {\n            IntPtr hwndToolTip = Misc.ProxySendMessage(_hwnd, NativeMethods.TB_GETTOOLTIPS, IntPtr.Zero, IntPtr.Zero);\n            return Misc.GetItemToolTipText(_hwnd, hwndToolTip, _idCommand);\n        }\n\n        private void Invoke()\n        {\n            // Make sure that the toolbar is enabled, and that the toolbar button is enabled.\n            if (!SafeNativeMethods.IsWindowEnabled(_hwnd)\n                || Misc.ProxySendMessageInt(_hwnd, NativeMethods.TB_ISBUTTONENABLED, new IntPtr(_idCommand), IntPtr.Zero) == 0)\n            {\n                throw new ElementNotEnabledException();\n            }\n\n            // Check that button can be clicked (button not hidden)\n            // This state could change anytime so success is not guaranteed\n            if (Misc.ProxySendMessageInt(_hwnd, NativeMethods.TB_ISBUTTONHIDDEN, new IntPtr(_idCommand), IntPtr.Zero) != 0)\n            {\n                throw new InvalidOperationException(SR.OperationCannotBePerformed);\n            }\n\n            // Click the center of the button\n            // TB_CHECKBUTTON and TB_PRESSBUTTON messages are not used as they will not trigger proper notifications\n            // Need to check that this button is visible to the mouse\n            Rect boundingRectangle = BoundingRectangle;\n\n            if (boundingRectangle.IsEmpty)\n            {\n                throw new InvalidOperationException(SR.OperationCannotBePerformed);\n            }\n\n            // make sure this item is active\n            SetFocus();\n\n            Misc.MouseClick(((int)boundingRectangle.Left + (int)boundingRectangle.Right) / 2, ((int)boundingRectangle.Top + (int)boundingRectangle.Bottom) / 2);\n        }\n","sourceCodeStart":634,"sourceCodeEnd":670,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsToolbar.cs#L634-L670","documentation":"WindowsToolbar's Invoke provider clicks a toolbar button by simulating a mouse click at the button's center. Before clicking it checks TB_ISBUTTONHIDDEN; if the button is reported hidden, the click cannot land on it, so an InvalidOperationException(SR.OperationCannotBePerformed) is thrown. UIA providers throw this when the requested operation is not currently possible for the element's live state.","triggerScenarios":"Calling Invoke() (InvokePattern) on a toolbar button proxy whose underlying Win32 button returns nonzero for TB_ISBUTTONHIDDEN (e.g. the toolbar was resized so the button overflowed into a chevron, or the button is otherwise hidden).","commonSituations":"Automating toolbars in windows that are too small, toolbars with overflow/chevron areas, or tests running against a UI whose layout changed between locating the element and invoking it.","solutions":["Resize the target window/toolbar (or collapse overflowing UI) so the button is fully visible before invoking.","Re-find the element right before invoking, since the hidden state can change at any time.","If the button lives in the overflow chevron, invoke the chevron first or use the element exposed for the overflow area.","Catch InvalidOperationException and retry after making the element visible."],"exampleFix":"// before\nbutton.Invoke(); // throws if hidden\n// after\nvar rect = button.Current.BoundingRectangle;\nif (rect.Width == 0 || rect.Height == 0)\n    window.SetWindowPosition/resize or open the overflow first;\nbutton.Invoke();","handlingStrategy":"validation","validationCode":"var r = button.Current.BoundingRectangle;\nif (r.IsEmpty || !button.Current.IsEnabled) throw new SkipException(\"toolbar button not clickable now\");","typeGuard":"bool IsInvokable(AutomationElement e) => e.Current.IsEnabled && !e.Current.BoundingRectangle.IsEmpty;","tryCatchPattern":"try { button.Invoke(); }\ncatch (InvalidOperationException) { /* make visible / resize window, then retry once */ }","preventionTips":["Check BoundingRectangle is non-empty before invoking toolbar buttons","Handle toolbar overflow/chevron layout before automation","Re-find elements immediately before invoking since state changes anytime"],"tags":["uiautomation","winforms","invoke-pattern","hidden-element"],"backgroundTag":"unsupported-operation","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"}