dotnet/wpf · error · InvalidOperationException
Win32 edit control cannot have a child element.
Error message
Win32 edit control cannot have a child element.
What it means
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.
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.
Example fix
// before
var range = textPattern.RangeFromChild(childElement); // always throws on edit boxes
// after
if (editBoxElement.FindFirstChild(TreeScope.Children, Condition.TrueCondition) == null)
{
var range = textPattern.DocumentRange; // edit boxes have no children
} Defensive patterns
Strategy: validation
Validate before calling
var hasChildren = el.FindFirstChild(TreeScope.Children, Condition.TrueCondition) != null;
if (!hasChildren) { var range = textPattern.DocumentRange; } Try / catch
try { return textPattern.RangeFromChild(child); }
catch (InvalidOperationException) { return textPattern.DocumentRange; } Prevention
- Know that Win32 edit controls have no child elements.
- Only call RangeFromChild on controls that embed objects.
- Use DocumentRange or RangeFromPoint for edit boxes.
When it happens
Trigger: Calling TextPattern.RangeFromChild with any IRawElementProviderSimple obtained under an edit box's automation element (e.g. from UIA tree traversal).
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- The value of argument 'unit
- ' ' is not a valid value for this control.
- Character offset not valid in TextRange.
- new ArgumentNullException(name)
- Operation cannot be performed.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/82c0933f84607601.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsEditBox.cs:343
ITextRangeProvider [] ITextProvider.GetSelection()
{
int start, end;
GetSel(out start, out end);
return new ITextRangeProvider[] { new WindowsEditBoxRange(this, start, end) };
}
ITextRangeProvider [] ITextProvider.GetVisibleRanges()
{
int start, end;
GetVisibleRangePoints(out start, out end);
return new ITextRangeProvider[] { new WindowsEditBoxRange(this, start, end) };
}
ITextRangeProvider ITextProvider.RangeFromChild(IRawElementProviderSimple childElement)
{
// we don't have any children so this call must be in error.
throw new InvalidOperationException(SR.Format(SR.EditControlsHaveNoChildren, GetType().FullName));
}
ITextRangeProvider ITextProvider.RangeFromPoint(Point screenLocation)
{
// convert screen to client coordinates.
// (Essentially ScreenToClient but MapWindowPoints accounts for window mirroring using WS_EX_LAYOUTRTL.)
NativeMethods.Win32Point clientLocation = (NativeMethods.Win32Point)screenLocation;
if (!Misc.MapWindowPoints(IntPtr.Zero, WindowHandle, ref clientLocation, 1))
{
return null;
}
// we have to deal with the possibility that the coordinate is inside the window rect
// but outside the client rect. in that case we just scoot it over so it is at the nearest
// point in the client rect.
NativeMethods.Win32Rect clientRect = new NativeMethods.Win32Rect();
if (!Misc.GetClientRect(WindowHandle, ref clientRect))
{View on GitHub (pinned to 81131a70a4)