dotnet/wpf · error · COMException

0x80040206

0x80040206

Error message

SR.TextStore_TS_E_NOLAYOUT

What it means

TS_E_NOLAYOUT (0x80040206) is thrown from GetPositionFromPoint when the TextView cannot validate layout for the transformed point — WPF has no valid screen layout for the text, so a screen coordinate cannot be mapped to a text position. TSF defines this HRESULT exactly for 'layout information is not available'.

Solutions

  1. Delay the text-service point query until the editor is loaded, rendered, and visible (IsLoaded/IsVisible true, layout complete).
  2. Ensure the control is in a visible window and the visual tree, then retry; TSF clients typically retry after TS_E_NOLAYOUT.
  3. If hit-testing is programmatically needed, force a layout pass (UpdateLayout) before requesting the position.

Example fix

// before
var pos = GetPositionFromPoint(screenPoint); // may throw TS_E_NOLAYOUT before layout
// after
if (textBox.IsLoaded && textBox.IsVisible) { textBox.UpdateLayout(); var pos = GetPositionFromPoint(screenPoint); }
Defensive patterns

Strategy: retry

Validate before calling

bool ready = editor.IsLoaded && editor.IsVisible; if (ready) editor.UpdateLayout(); // then issue the point query

Try / catch

catch (COMException e) when (e.ErrorCode == unchecked((int)0x80040206)) { /* TS_E_NOLAYOUT: defer/retry after layout completes */ }

Prevention

When it happens

Trigger: ITextStoreACP::GetACPFromPoint (GetPositionFromPoint) called before the control's layout has been produced/validated: view.Validate(milPoint) returns false after transforming the POINT with the current transform.

Common situations: TSF composition/point queries arriving before the RichTextBox is rendered (not yet measured/arranged), after the control was removed from the visual tree, or while the window is minimized/hidden.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/1c2f07b6b4e066eb. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextStore.cs:957

            GetVisualInfo(out source, out win32Window, out view);
            compositionTarget = source.CompositionTarget;

            // Convert to client coordinates.
            SafeNativeMethods.ScreenToClient(new HandleRef(null, win32Window.Handle), ref point);

            // Convert to mil measure units.
            milPoint = new Point(point.x, point.y);
            milPoint = compositionTarget.TransformFromDevice.Transform(milPoint);

            // Convert to local coordinates.
            GeneralTransform transform = compositionTarget.RootVisual.TransformToDescendant(RenderScope);
            // REVIEW: should we throw if the point could not be transformed?
            transform?.TryTransform(milPoint, out milPoint);

            // Validate layout information on TextView
            if (!view.Validate(milPoint))
            {
                throw new COMException(SR.TextStore_TS_E_NOLAYOUT, UnsafeNativeMethods.TS_E_NOLAYOUT);
            }

            // Do the hittest.
            position = view.GetTextPositionFromPoint(milPoint, (flags & UnsafeNativeMethods.GetPositionFromPointFlags.GXFPF_NEAREST) != 0 /* snapToText */);
            if (position == null)
            {
                // GXFPF_ROUND_NEAREST was clear and we didn't hit a char.
                throw new COMException(SR.TextStore_TS_E_INVALIDPOINT, UnsafeNativeMethods.TS_E_INVALIDPOINT);
            }

            positionCP = position.CharOffset;
            if ((flags & UnsafeNativeMethods.GetPositionFromPointFlags.GXFPF_ROUND_NEAREST) == 0)
            {
                // Check if the point is on the backward position of the TextPosition.
                Rect rectCur;
                Rect rectPrev;
                Point milPointTopLeft;
                Point milPointBottomRight;

View on GitHub (pinned to 81131a70a4)