question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:

  1. OnLoad.
  2. SetVisibleCore(true) 🤔

The form is removed from Application.OpenForms in two cases:

  1. OnFormClosed.
  2. 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

FormCloseTest.zip

  1. Build.
  2. Run.
  3. Press button2 => Application.OpenForms will contains 3 forms (main, mdiParent and mdiChild).
  4. Press button3 => Application.OpenForms will contains 1 form - main.

Issue Analytics

  • State:closed
  • Created 5 months ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
elachlancommented, Apr 24, 2023

Probably, but I’d try and keep them as separate commits and make sure the merge isn’t done via a squash.

1reaction
MandiMancommented, May 31, 2023

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found