{"record":{"id":"65dcce2fa29ec2ba","repo":"dotnet/wpf","slug":"sr-operationcannotbeperformed-windowstreeview","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/WindowsTreeView.cs","lineNumber":610,"sourceCode":"\n            // Begins in-place editing of the specified item's text, replacing the text of the item with a single-line\n            // edit control containing the text. This message implicitly selects and focuses the specified item.\n            IntPtr hwndEdit = Misc.ProxySendMessage(hwnd, NativeMethods.TVM_EDITLABELW, IntPtr.Zero, item);\n\n            if (hwndEdit == IntPtr.Zero)\n            {\n                // assume that the hwnd was bad\n                throw new ElementNotAvailableException();\n            }\n\n            // Now set the text to the edit control\n            // Note: The lParam of the WM_SETTEXT is NOT a receive parameter. Just used this overloaded version\n            // of ProxySendMessage() for convinces.\n            if (Misc.ProxySendMessageInt(hwndEdit, NativeMethods.WM_SETTEXT, IntPtr.Zero, new StringBuilder(text)) != 1)\n            {\n                // Cancel the edit.\n                Misc.ProxySendMessage(hwnd, NativeMethods.TVM_ENDEDITLABELNOW, (IntPtr)1, IntPtr.Zero);\n                throw new InvalidOperationException(SR.OperationCannotBePerformed);\n            }\n\n            // TVM_ENDEDITLABELNOW ends the editing of a tree-view item's label.\n            // The wParam indicates whether the editing is canceled without being saved to the label.\n            // If this parameter is TRUE, the system cancels editing without saving the changes.\n            // Otherwise, the system saves the changes to the label.\n            Misc.ProxySendMessage(hwnd, NativeMethods.TVM_ENDEDITLABELNOW, IntPtr.Zero, IntPtr.Zero);\n\n            // Need to give some time for the control to do all its proceeing.\n            bool wasTextSet = false;\n            for(int i=0; i < 10; i++)\n            {\n                System.Threading.Thread.Sleep(1);\n\n                // Now see if the treeviewitem really got set.\n                if (text.Equals(WindowsTreeView.GetItemText(hwnd, item)))\n                {\n                    wasTextSet = true;","sourceCodeStart":592,"sourceCodeEnd":628,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsTreeView.cs#L592-L628","documentation":"After TVM_EDITLABELW opens the in-place edit control, SetItemText sets the text with WM_SETTEXT. If WM_SETTEXT does not return 1 (the text was not applied), the provider cancels the edit (TVM_ENDEDITLABELNOW with cancel=TRUE) and throws InvalidOperationException(SR.OperationCannotBePerformed).","triggerScenarios":"ValuePattern.SetValue() on a tree-view item where the WM_SETTEXT to the in-place edit control fails — e.g. the edit handle became invalid mid-operation or the control rejected the text (memory/handle pressure, text filter).","commonSituations":"Setting values on tree items in unstable UIs where the label edit session is terminated by the app between opening the edit and setting text; long strings or invalid characters rejected by the app's edit control.","solutions":["Retry the SetValue after re-acquiring the element.","Ensure the tree item remains selected and visible during the edit operation.","Check the text for characters or length the application's edit control rejects.","Catch InvalidOperationException and fall back to another editing mechanism exposed by the app."],"exampleFix":"// before\nitem.SetValue(text); // may throw if WM_SETTEXT fails\n// after\ntry { item.SetValue(text); }\ncatch (InvalidOperationException) { Thread.Sleep(200); RefreshElement(); item.SetValue(text); }","handlingStrategy":"retry","validationCode":"if (!item.Current.IsEnabled || !treeWindow.Current.IsEnabled) throw new SkipException(\"tree busy\");","typeGuard":null,"tryCatchPattern":"try { item.SetValue(text); }\ncatch (InvalidOperationException) { Thread.Sleep(200); item = ReFind(item); item.SetValue(text); }","preventionTips":["Keep the target item selected and visible during SetValue","Retry transient failures with short backoff","Avoid mutating labels while the app is performing background edits"],"tags":["uiautomation","winforms","treeview","setvalue"],"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"}