如何编写表单中的关闭按钮? [英] How to code the close button in the form?
本文介绍了如何编写表单中的关闭按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我可以问一下是否有人对如何在表单的关闭"按钮中编写代码有想法.
例如,如果用户单击关闭按钮(带有X的红色框),那么我想显示另一种形式.
谢谢!
Hi Guys,
Can I ask if someone have an idea on how can I write a code in the close button in the form.
Example, if the user click the close button (the red box with X) then another form that i want be show.
Thanks!
推荐答案
我假设您在这里谈论WinForms.如果是这样,则每个窗口都具有与之关联的Closing and Closed事件.我要做的是重写OnClosed实现,并从那里显示我的窗口.
I assume you are talking about WinForms here. If you are, each window has a Closing and Closed event associated with it. What I would do is override the OnClosed implementation and show my window from there.
protected override void OnClosed(EventArgs args)
{
new NewWindow().Show();
base.OnClosed(args);
}
从您的角度来看,重要的是,在应用程序退出时不会调用此方法,因此不会使流氓窗口意外打开.
Importantly from your point of view, this method is not called when the application exits, so you won''t get rogue windows opening up unexpectedly.
附加任何所需的内容到表单的 OnClosing [ ^ ]事件.这样,您就不必区分导致表单关闭的原因("X",Alt + F4,右键单击任务栏,...).
Attach whatever you want to the form''s OnClosing[^] event. That way you don''t have to differentiate what causes the form to close ("X", Alt + F4, right-click on Taskbar, ...).
override void OnClosing()
{
SomeOtherForm otherForm = new SomeOtherForm(whatever);
otherForm.Show();
}
这篇关于如何编写表单中的关闭按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文