Major regression in Form.ShowDialog(IWin32Window) in .NET 7 -- ship blocker for Paint.NET 5.0
See original GitHub issue.NET version
7.0.0
Did it work in .NET Framework?
Yes
Did it work in any of the earlier releases of .NET Core or .NET 5+?
Yes, it worked fine in .NET 5 and .NET 6
Issue description
When Form.ShowDialog(IWin32Window)
is called with a Control
instead of a Form
, the dialog’s parenting/ownership is not set correctly, causing the dialog to not “track” with the parent form correctly.
The most visible symptom of this is that if you switch between virtual desktops, the parent form is correctly hidden but the dialog is not. And, it is possible to interact with the main window in certain ways. In my app (Paint.NET), it is possible to switch between images by using the taskbar (kind of like how IE/Edge used to let you switch between tabs). This is normally prevented when a modal dialog is active, but right now it’s not. This can result in actions affecting the wrong image, which then corrupts the state of the image’s change history and causes data loss.
Another symptom is that the Owner
for the dialog is null
, even though it should probably be control.FindForm()
This is currently a ship-blocker for Paint.NET 5.0 on .NET 7. I cannot release the new version with this bug.
Known workaround: use dialog.ShowDialog(control.FindForm()
instead of dialog.ShowDialog(control)
. However, this requires changes to many places in the code, and is not a fix I can apply to libraries or plugins.
I believe this regressed due to https://github.com/dotnet/winforms/pull/3394 cc @dreddy-work which changed whether Owner
was set or SetWindowLong(..., GWL.HWNDPARENT, ...)
was called. Previously, SetWindowLong
was always called.
Steps to reproduce
Create a Form and do Application.Run(form)
. Make sure the Form has at least 1 child control, like a button
.
In some code within the form, like in the button
’s click handler, create another Form and call dialog.ShowDialog(button)
While the dialog is open, switch between virtual desktops using WinKey+Shift+Left/Right
Issue Analytics
- State:
- Created 10 months ago
- Comments:17 (10 by maintainers)
Top GitHub Comments
Tested this issue in .Net 6.0, 7.0 and the latest 8.0 builds using above sample application: can reproduce it in .Net 7.0 and 8.0. ShowDialogBug.zip
.Net 6.0:
.Net 7.0 and 8.0:
I built my own version of System.Windows.Forms.dll with the fix, and I can thankfully confirm that it does fix the issues I was seeing in Paint.NET
Looks like I’ll have to ship my own S.W.F.dll for the time being, but this is preferable to rolling back to .NET 6. phew!