简体   繁体   English

X C# 上的表格关闭

[英]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#.我有一个简单的应用程序,2 forms .NETv3.5 C#。

The application loads the first form, i press 'X' on the windows bar.应用程序加载第一个表单,我在 windows 栏上按“X”。

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.按下 X 时不会调用主窗体 Closing 方法,因为按下 x 时窗体被隐藏。 Form.Deactivated is called but not Form.Closing. Form.Deactivated 被调用,但不是 Form.Closing。

How can i catch the the even when the X is pressed?即使按下 X,我如何才能捕捉到? (Then I can implement Application.Exit()) (然后我可以实现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.我猜在Program.cs中,您调用Application.Run将主表单作为参数传递。 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.要在按下 X 按钮时捕获事件,请将事件处理程序添加到辅助窗体的Closed事件,而不是主窗体的。 In that event, call main form's Close method (or Application.Exit ).在这种情况下,调用主窗体的Close方法(或Application.Exit )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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