Add AppContext switch to mirror Owner.TopMost for modal dialogs (restore .NET Framework‑like behavior)#14138
Open
LeafShi1 wants to merge 3 commits intodotnet:mainfrom
Open
Conversation
…ore .NET Framework‑like behavior)
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds an AppContext switch System.Windows.Forms.MirrorTopMostForModalDialogs to restore .NET Framework-like behavior where modal dialogs temporarily inherit the TopMost property from their owner Form during the modal session.
Key Changes:
- Introduces a new AppContext switch that when enabled, automatically sets TopMost=true on modal dialogs if their owner Form has TopMost=true
- Restores the original TopMost state after the dialog closes
- Default behavior is false to maintain current .NET behavior
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs |
Adds the MirrorTopMostForModalDialogs switch definition and property |
src/System.Windows.Forms/System/Windows/Forms/Form.cs |
Implements the TopMost mirroring logic in ShowDialog method, capturing original state and restoring it after the modal session |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #14138 +/- ##
===================================================
+ Coverage 76.01271% 77.15426% +1.14154%
===================================================
Files 3279 3279
Lines 645325 645337 +12
Branches 47720 47722 +2
===================================================
+ Hits 490529 497905 +7376
+ Misses 145491 143738 -1753
+ Partials 9305 3694 -5611
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #14101
Proposed changes
System.Windows.Forms.MirrorTopMostForModalDialogs.When enabled,
ShowDialogwill temporarily set the dialog asTopMostif its owner isTopMost, and restore the original state after closing.Default is false to keep current behavior.
Customer Impact
TopMostforms will stay above the owner during the modal session, matching .NET Framework behavior.Regression?
Risk
Screenshots
Before
If the owner is TopMost and the dialog is not, the dialog can be pushed behind the owner during the modal session.

After
Modal dialogs temporarily become TopMost when the owner is TopMost, and stay above the owner until closed.
Test methodology
Test environment(s)
Microsoft Reviewers: Open in CodeFlow