简体   繁体   中英

Form Closing on X C#

After an immense amount of reasearch I am still no closer to a solution.

I have a simple app, 2 forms .NETv3.5 C#.

The application loads the first form, i press 'X' on the windows bar.

The application hides the form and continues to run without exiting the application even though the only form loaded is closed.

The main form Closing method does not get called when the X is pressed because the form is hidden when the x is pressed. Form.Deactivated is called but not Form.Closing.

How can i catch the the even when the X is pressed? (Then I can implement Application.Exit())

Check if you have timer or thread that is running. If so stop those.

I guess in Program.cs , you call Application.Run passing the main form as parameter. If so, the app won't exit until the main form closes.
To catch the event when X button is pressed, add event handler to secondary form's Closed event, not the main form's. In that event, call main form's Close method (or Application.Exit ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM