Incorrect Z-Order behavior for popup forms in .NET compared to .NET Framework when using SetWindowPos with HWND_TOPMOST
.NET version
.NET SDK: Version: 10.0.100 Commit: b0f34d51fc Workload version: 10.0.100-manifests.5fb86115 MSBuild version: 18.0.2+b0f34d51f
Did it work in .NET Framework?
Yes
Did it work in any of the earlier releases of .NET Core or .NET 5+?
Didn't check it.
Issue description
When showing a custom dialog-form from a "pop-up" in Windows Forms on .NET, the Z-Order behaves differently compared to .NET Framework. Specifically:
In .NET Framework, calling SetWindowPos with HWND_TOPMOST (-1) works as expected—the popup does not stay topmost permanently. In .NET, the pop-up window remains topmost always, causing UI elements like color pickers or other dialogs to appear behind the pop-up.
This leads to incorrect layering and usability issues in scenarios where multiple pop-ups or dialogs are involved.
Steps to reproduce
In the attachments, you can find two projects: WinFormsApp9 - it's .NET application WindowsFormsApp1 - it's .NET FW application
To reproduce the issue:
- Run the .NET project
- Press
button1- it will open aspecialpop-up window - Press
button1again - it will open Form, using theShowDialogmethod. As you can see, this form shows behind the pop-up.
@Olina-Zhang can you find out if this has regressed and if so, when?
I was able to repro it with .NET 6.0. @Olina-Zhang can you please try one of the older versions?
@John-Hart @merriemcgaw, the issue does reproduce on .NET 5.0, not reproduce on .NET Core 3.1 as below screenshot.
Thanks @Zheng-Li01! @LeafShi1 can you investigate changes in this area from way back then? It is definitely something that changed between 3.1 and 5.0.
Could this be related to #6190 ?
and another issue https://github.com/dotnet/winforms/issues/9351
As a workaround, adding form.TopMost = true for pop-up window
In .NET Framework, ShowDialog will propagate the TopMost property from the main form to the popup form.
However, in modern .NET, this behavior has changed — ShowDialog does not automatically inherit the TopMost setting, so the popup form will not be topmost unless explicitly set.
Is this switch available in VB startup process? Is there a way to set it from VB?
Is this switch available in VB startup process? Is there a way to set it from VB?
Yes, it can also be used in VB projects, simply set "System.Windows.Forms.MirrorTopMostForModalDialogs": true in the app.runtimeconfig.json file.