Form is removed from Application.OpenForms after RecreateHandle
See original GitHub issue.NET version
.Net6, .Net7 checked.
Did it work in .NET Framework?
No
Did it work in any of the earlier releases of .NET Core or .NET 5+?
I checked only .Net6, .Net7 but according to the code, problem exist in all versions (.net framework too).
Issue description
While working on https://github.com/dotnet/winforms/issues/3606 I found that after calling to RecreateHandle, the form is completely removed from Application.OpenForms.
The form is added to Application.OpenForms in two cases:
OnLoad.SetVisibleCore(true)🤔
The form is removed from Application.OpenForms in two cases:
OnFormClosed.OnHandleDestroyed🤔🤷♂️
Here we clearly see that after RecreateHandle we will completely lost our form from Application.OpenForms 😦 We call RecreateHandle for the form in several cases (ShowInTaskbar \ RightToLeft change etc…).
I still can’t say for sure how much duplication of the Application.OpenForms.Remove call in OnHandleDestroyed is necessary.
Steps to reproduce
- Build.
- Run.
- Press button2 =>
Application.OpenFormswill contains 3 forms (main, mdiParent and mdiChild). - Press button3 =>
Application.OpenFormswill contains 1 form - main.
Issue Analytics
- State:
- Created 5 months ago
- Comments:8 (7 by maintainers)
Top Results From Across the Web
Closing of all Previous Forms and Opening of New Form in ...
Two approaches. First is using Application.OpenForms like this: foreach (Form form in Application.OpenForms) { if(Form is ...
Read more >Application.OpenForms Property (System.Windows.Forms)
The OpenForms property represents a read-only collection of forms owned by the application. This collection can be searched by index position or by...
Read more >WindowsFormsApplicationBase.OpenForms Property
The My.Application.OpenForms property gets a collection of all the application's open forms. The behavior is identical to the Application.OpenForms property.
Read more >closing all the window forms
Try as below code to close all Open Forms of your application. C#. foreach (Form form in Application.OpenForms) { form.Close(); }
Read more >Configurator - Form Styling Properties - ERP 10
Is there a way to get to the form object variable inside a configurator (this)? Or a way to get to the form...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Probably, but I’d try and keep them as separate commits and make sure the merge isn’t done via a squash.
Verified this on .NET 8.0 TP build: 8.0.100-preview.5.23276.4, issue was fixed, test result is same as above video.