如何从Main表单对象C#winforms中关闭所有子表单对象 [英] How to close all the childforms objects from Main form object C# winforms

查看:116
本文介绍了如何从Main表单对象C#winforms中关闭所有子表单对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有以下要求。



i已经创建了主表单说登录表单。用户登录后我通过点击邮件表单中的登录按钮事件创建了一个名为接待员表单的表单。


来自Admin Form的
我已经调用了邮件表单(来自admin的注销类型)并返回主要形式)。在ExitButton事件的主要形式中,我调用了this.Dispose()并且代码没有正常终止。我期待所有打开的表单都将被关闭,但我不是从程序中释放它只是在调试模式。

Hi ,

I have following requirement.

i have created the Main form say login form. Once the user is logged in . i have created one more form called receptionist form by clicking the login button event from mail form.

from Admin Form i have called the mail form(kind of signout from admin and returning back to main form). In main form on ExitButton event i have called this.Dispose() and the code is not gracefully terminated. I was expecting that all the open forms will be closed, but i am not release from the program and it is in debug mode only.

推荐答案

除了解决方案1 2:



我不相信您的任何表格是儿童表格,或者任何表格都是其他表格的父母,除非您使用MDI(我看起来不像你这样做。在Forms中,父子关系(由属性 System.Windows.Forms.Control.Parent 定义)有效地完成了:如果您尝试分配表单的父级,并抛出异常。这可以通过将属性 TopLevel 指定为false来解决,但它没有实际意义。



表单确实拥有所有者拥有的重要关系( Form.Owner ),并且一个表单不同:主要表单。这是在 Application.Start 的调用中使用的表单实例。关闭此表单时,应用程序存在。这可以作为另一种技术来显示一些起点:你的一个表单调用 Application.Start ,然后关闭它并使应用程序退出,然后再次启动应用程序与其他形式。但是使用模态形式(解决方案1)通常可以简单得多。这是如下:

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



-SA
In addition to Solutions 1 and 2:

I don't believe any of your forms are child forms, or any of the forms are the parents of some other forms, unless you use MDI (I doesn't look like you do). In Forms, the parent-child relationship (defined by the property System.Windows.Forms.Control.Parent) is effectively done defunct: if you try to assign a form's parent, and exception will be thrown. This can be worked around by assigning the property TopLevel to false, but it does not make practical sense.

The forms do have important relationship owner-owned (Form.Owner) and one form is different: the main one. This is the form instance which was used in the call to Application.Start. When this form is closed, application exists. This can be used as one more technique to show some "starting" point: your call Application.Start with one form, then close it and make application exit, then start application again with some other form. But the use of the modal form (Solution 1) could usually be much simpler. This is how:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showdialog%28v=vs.110%29.aspx[^].

—SA


this 是这种情况下的当前表单。直接处理当前表单并不明智,但调用 Close()也会默认处理它。尽管如此,如果没有其他正确设置的子表单打开,那么只有一个单一形式的主表单将关闭该表单将退出应用程序。

您可以通过调用 Application.Exit(); [ ^ ]。您也可以致电 Environment.Exit(); [ ^ ],但是更严重。
this is the current form in this case. It is not wise to dispose current form directly, but calling Close() will also dispose it by default. Still, as only a singel form is main form closing that one will exit the application if no other properly set up child form is open.
You can exit the application by calling Application.Exit();[^]. You can also call Environment.Exit();[^], but taht's more drastic.


调用 this.Dispose 将销毁调用它的对象和让你的申请处于不确定状态。



目前尚不清楚你的表格是如何连接的,但我建议更好的模式是使用 DialogForm 作为登录,并在登录成功后让主表单继续。
Calling this.Dispose will destroy the object that calls it and leave your application in an indeterminate state.

It is not clear exactly how your forms are connected, but I would suggest a better model would be to use a DialogForm as the login, and leave the main form to continue after the login succeeds.


这篇关于如何从Main表单对象C#winforms中关闭所有子表单对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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