{"record":{"id":"82c0933f84607601","repo":"dotnet/wpf","slug":"win32-edit-control-cannot-have-a-child-element","errorCode":null,"errorMessage":"Win32 edit control cannot have a child element.","messagePattern":"Win32 edit control cannot have a child element\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsEditBox.cs","lineNumber":343,"sourceCode":"        ITextRangeProvider [] ITextProvider.GetSelection()\n        {\n            int start, end;\n            GetSel(out start, out end);\n            return new ITextRangeProvider[] { new WindowsEditBoxRange(this, start, end) };\n        }\n\n        ITextRangeProvider [] ITextProvider.GetVisibleRanges()\n        {\n            int start, end;\n            GetVisibleRangePoints(out start, out end);\n\n            return new ITextRangeProvider[] { new WindowsEditBoxRange(this, start, end) };\n        }\n\n        ITextRangeProvider ITextProvider.RangeFromChild(IRawElementProviderSimple childElement)\n        {\n            // we don't have any children so this call must be in error.\n            throw new InvalidOperationException(SR.Format(SR.EditControlsHaveNoChildren, GetType().FullName));\n        }\n\n        ITextRangeProvider ITextProvider.RangeFromPoint(Point screenLocation)\n        {\n            // convert screen to client coordinates.\n            // (Essentially ScreenToClient but MapWindowPoints accounts for window mirroring using WS_EX_LAYOUTRTL.)\n            NativeMethods.Win32Point clientLocation = (NativeMethods.Win32Point)screenLocation;\n            if (!Misc.MapWindowPoints(IntPtr.Zero, WindowHandle, ref clientLocation, 1))\n            {\n                return null;\n            }\n\n            // we have to deal with the possibility that the coordinate is inside the window rect\n            // but outside the client rect. in that case we just scoot it over so it is at the nearest\n            // point in the client rect.\n            NativeMethods.Win32Rect clientRect = new NativeMethods.Win32Rect();\n            if (!Misc.GetClientRect(WindowHandle, ref clientRect))\n            {","sourceCodeStart":325,"sourceCodeEnd":361,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsEditBox.cs#L325-L361","documentation":"WindowsEditBox implements ITextProvider but a Win32 edit control has no child elements, so ITextProvider.RangeFromChild always throws InvalidOperationException(EditControlsHaveNoChildren). Any childElement argument is by definition invalid for this control.","triggerScenarios":"Calling TextPattern.RangeFromChild with any IRawElementProviderSimple obtained under an edit box's automation element (e.g. from UIA tree traversal).","commonSituations":"Generic UIA traversal code that calls RangeFromChild for every TextPattern element without checking whether the element actually has children; frameworks that treat all TextPattern providers uniformly.","solutions":["Do not call RangeFromChild on edit-box TextPatterns; use DocumentRange/RangeFromPoint or selection ranges instead.","Guard by checking the element has children (FindFirstChild != null) before calling RangeFromChild.","Catch InvalidOperationException and treat the range as the document range.","For embedded objects, use a control that truly hosts children (e.g. rich text) rather than a Win32 edit box."],"exampleFix":"// before\nvar range = textPattern.RangeFromChild(childElement); // always throws on edit boxes\n// after\nif (editBoxElement.FindFirstChild(TreeScope.Children, Condition.TrueCondition) == null)\n{\n    var range = textPattern.DocumentRange; // edit boxes have no children\n}","handlingStrategy":"validation","validationCode":"var hasChildren = el.FindFirstChild(TreeScope.Children, Condition.TrueCondition) != null;\nif (!hasChildren) { var range = textPattern.DocumentRange; }","typeGuard":null,"tryCatchPattern":"try { return textPattern.RangeFromChild(child); }\ncatch (InvalidOperationException) { return textPattern.DocumentRange; }","preventionTips":["Know that Win32 edit controls have no child elements.","Only call RangeFromChild on controls that embed objects.","Use DocumentRange or RangeFromPoint for edit boxes."],"tags":["uiautomation","textpattern","editbox","invalid-argument"],"backgroundTag":"invalid-argument-value","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"}