In the case of multiple windows in the application, the application may be challenging when the application terminates when the starting window is closed. This article discusses the correct way to deal with this situation.
Preliminary attempt
Initially, you can try to use the following code to close Form1 and display Form2:
However, when Form1 is closed, this will cause Form2 to be closed. In order to overcome this problem, you can try to hide Form1 instead of closing it:
1 2 3 |
|
<决> Solution
1 2 3 |
|
In order to solve these two problems, the solution is to modify the code in the Program.cs file, which defines the application of the application in this file. By adjusting this code, you can prevent the application termination when you close the starting window.
In the following modified code, add the event processing program to the startup window to monitor its closure event:
When the startup window is turned off, the formclosed event processing program will be triggered:
This incident processing procedure ensures that the application exits only when there are no other windows. When the window outside the startup window is closed, the event processing program will update the event listener to monitor the closure of the next open window.
1 2 3 4 5 6 7 8 9 |
|
By implementing this solution, you can now close the startup window without terminating the application, which allows other windows to keep the activity status and allow the application to run as expected.
The above is the detailed content of How to Prevent Application Closure When Closing the Startup Form?. For more information, please follow other related articles on the PHP Chinese website!