在Windows关闭(X)按钮上打开另一个窗体 [英] Open another form on windows close(X) button

查看:64
本文介绍了在Windows关闭(X)按钮上打开另一个窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有两个带按钮的winforms。我可以通过按钮在表单之间导航。现在我想捕捉窗口关闭(X)按钮导航到下一个窗体。但当我处理窗口关闭或关闭事件时,项目停止。请帮帮我。

Thaks

Hi All,
I Have two winforms with buttons. I'm able to navigate between forms though buttons. Now i want to capture windows close(X) button to navigate to next form. But when I handle the windows close or closing events the projects stops. Please help me with this.
Thaks

推荐答案

关闭主窗体时 - 应用程序启动时打开的窗体,通过在program.cs文件中调用Application.Run - 应用程序关闭,你可以做很多事情(除了在Main方法中创建新应用程序的一些讨厌的回合)。



所以在用户完成之前不要关闭主窗体:改为隐藏它并使用ShowDialog显示第二种形式:

When you close the main form - the one that is opened when you application starts, via the call to Application.Run in your program.cs file - the application closes and there isn't a lot you can do about that (other than some nasty playing round in the Main method to create new Applications).

So don't close the main form until the user is finished: Hide it instead and use ShowDialog to display the second form:
MySecondForm msf = new MySecondForm();
Hide();
msf.ShowDialog();
Show();



这样,当你的第二个表单关闭时,你的第一个表单再次显示,用户仍然可以关闭主表单以退出应用程序。


That way, when your second form closes, your first displays again, and the user can still close the main form to exit the application.


如果你想实现导航,请询问导航的实现,而不是关闭和显示表格。



可能是实现逐帧导航的最佳方式和许多不同类型的导航仅使用一种形式。只需在每个步骤的表单上显示不同的内容。在最简单的情况下,您可以使用 DockStyle.Fill 停靠多个 Panel 实例(无论如何,如果Forms应该是停靠,以确保稳定和良好的可维护布局),一次只显示一个。导航可以隐藏当前面板,显示另一个面板。然后导航控件本身应该始终可见。



请参阅:

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.dock%28v=vs.110 %29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.windows.forms.dockstyle%28v=vs.110%29.aspx [ ^ ]。



要回到几种形式,问题是:如果您关闭表单,则无法显示再次;如果你关闭主窗体,它将退出应用程序。因此,如果您将其用于导航(同样,我不推荐它),您应该隐藏表单而不是关闭。您根本不需要[x]非客户端控制框,但为了防止关闭表单,您可以通过以下方式隐藏操作:覆盖事件 FormClosing 或覆盖虚方法 Form.OnFormClosing 然后取消关闭。这是通过将事件参数'property Cancel 赋值为true来完成的,但仅当 CloseReason 等于<$ c $时c> CloseReason.UserClosing ,允许因其他原因关闭。改为隐藏表格。



https://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs%28v=vs.11 0%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel(v = vs.110)的.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason%28v=vs.110%29.aspx [< a href =https://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason%28v=vs.110%29.aspx\"target =_ blanktitle =新窗口> ^ ],

https://msdn.microsoft.com/en-us/library/system.windows .forms.closereason%28v = vs.110%29.aspx [ ^ ]。



知道这种技术在许多方面很有用但是,再次使用多种形式进行导航并不是我推荐的。更好地使用单一形式,改变内容的可见性。或者,或者/另外,动态地为不同的帧重新填充控件。



-SA
If you want to implement navigation, ask about implementation of navigation, not about closing and showing forms.

Probably the best way to implement frame-by-frame kind of navigation and many different kinds of navigation is using only one form. Simply show different content on the form on each step. In simplest case, you can have several instanced of Panel docked using DockStyle.Fill (anyway, everything if Forms should be docked, to ensure stable and well maintainable layout), showing only one at a time. Navigation can hide current panel, showing another one. Then navigation controls themselves should of course be always visible.

Please see:
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.dock%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.dockstyle%28v=vs.110%29.aspx[^].

To come back to several forms, the problems are: if you close a form, you cannot show it again; and if you close main form, it will exit the application. Therefore, if you use this for navigation (again, I don't recommend it), you should hide forms instead of closing. You don't need [x] non-client control box at all, but to prevent closing a form, you can replace the action with hiding in the following way: override the event FormClosing or override the virtual method Form.OnFormClosing and then cancel closing. It's done by assigning of the event arguments' property Cancel to true, but only if CloseReason equals to CloseReason.UserClosing, to allow closing by other reasons. Hide the form instead.

https://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.closereason%28v=vs.110%29.aspx[^].

Knowing this technique is useful in many cases, but, again, using multiple forms for navigation is not what I recommend. Better use a single form with changed visibility of content. Or, alternatively/additionally, re-populate the controls in different ways for different frames on the fly.

—SA


编写代码以在表格结束事件
Write the code to open next form on form closing event


这篇关于在Windows关闭(X)按钮上打开另一个窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆