Skip to content

Fix AnchorLayoutV2 deferred anchor initialization when replacing anchored controls during suspended layout#14505

Open
LeafShi1 wants to merge 1 commit intodotnet:mainfrom
LeafShi1:Fix_14500_AnchorLayoutV2_breaks_anchored_layout_issue
Open

Fix AnchorLayoutV2 deferred anchor initialization when replacing anchored controls during suspended layout#14505
LeafShi1 wants to merge 1 commit intodotnet:mainfrom
LeafShi1:Fix_14500_AnchorLayoutV2_breaks_anchored_layout_issue

Conversation

@LeafShi1
Copy link
Copy Markdown
Member

@LeafShi1 LeafShi1 commented Apr 29, 2026

Fixes #14500

Root Cause

Under AnchorLayoutV2, when an anchored control is replaced while its parent container is inside a SuspendLayout / ResumeLayout flow, anchor metadata initialization can be deferred.

During ResumeLayout -> PerformLayout, the parent temporarily sets LayoutSuspendCount to 1 as part of its internal layout execution. The existing AnchorLayoutV2 logic treated that state the same as an externally suspended layout state, so the newly added anchored control could still skip AnchorInfo initialization even though actual layout was already running.

Proposed changes

  • Ensure AnchorLayoutV2 initializes missing AnchorInfo for anchored children immediately before anchored layout is applied, if that metadata is still missing.
  • Update the internal AnchorLayoutV2 path so that real layout execution can bypass the deferred-layout guard when it is running inside the framework’s internal layout pass.
  • Add a regression test that matches the customer repro pattern of replacing a Panel at runtime and verifies that the replacement panel continues to resize with the form, in AnchorLayoutTests.cs.

Customer Impact

  • After this fix, apps using AnchorLayoutV2 that replace an anchored control at runtime inside a SuspendLayout / ResumeLayout block will have the replacement control resize correctly with the form. The manual Anchor toggle workaround is no longer needed.

Regression?

  • No

Risk

  • Minimal

Screenshots

Before

When using AnchorLayoutV2. resizing the window, one of the inner panels is resized incorrectly if replaced by an equivalent panel after the InitializeComponent().

BeforeChanges.mp4

After

Replaced anchored controls continue to resize correctly with the form.

AfterChanges.mp4

Test methodology

  • Manually and unit test

Test environment(s)

  • .net 11.0.0-preview.5.26227.104
Microsoft Reviewers: Open in CodeFlow

@LeafShi1 LeafShi1 requested a review from a team as a code owner April 29, 2026 10:00
@LeafShi1 LeafShi1 added the waiting-review This item is waiting on review by one or more members of team label Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-area-label waiting-review This item is waiting on review by one or more members of team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[WinForms][AnchorLayoutV2] Updating some panels hierarchy breaks the anchored layout

1 participant