Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.

Call Application.Run passing ApplicationContext, not MainForm #50

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

PreferLinux
Copy link

Passing MainForm prevents WindowsFormsApplicationBase.ShutdownStyle from having any effect. It will always shut down when MainForm closes. This fixes that by passing the WindowsFormsApplicationContext, which handles OnMainFormClosed in a way that considers ShutdownStyle.

Passing MainForm prevents WindowsFormsApplicationBase.ShutdownStyle from
having any effect. It will always shut down when MainForm closes. This
fixes that by passing the WindowsFormsApplicationContext, which handles
OnMainFormClosed in a way that supports ShutdownStyle.
@PreferLinux
Copy link
Author

Imports System.Windows.Forms
Imports Microsoft.VisualBasic.ApplicationServices

Namespace ShutdownStyleTest
	Public Class ShutdownStyleTest
		Inherits Form
		
		Public Class winformsapp
			inherits WindowsFormsApplicationBase
			Public Sub New()
				MainForm = New ShutdownStyleTest()
				ShutdownStyle = ShutdownMode.AfterAllFormsClose
			End Sub
		End Class
		
		Public Shared Sub Main(args() as String)
			Dim appBase As New winformsapp()
			appBase.Run(args)
		End Sub
		
		Public Sub New()
			Text = "Form 1"
		End Sub
		
		Protected Overrides Sub OnClick(e As EventArgs)
			MyBase.OnClick(e)
			Dim frm as New Form()
			frm.Text = "Form 2"
			frm.Show()
		End Sub
	End Class
End Namespace

Here's a quick example test for the problem. Run the above, click on the form to get a second form, and then close the first form. The correct behaviour is that, due to ShutdownStyle = ShutdownMode.AfterAllFormsClose, only the first form closes while all others remain open. Without the fix in this pull request, all forms close.

Base automatically changed from master to main March 1, 2021 15:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant