亲子形式关系 [英] Parent Child Form Relationship

查看:117
本文介绍了亲子形式关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种形式

表格A(父母)
表格B(儿童)

1.问题是当表格B显示时,将表格B设为父级,将表格A设为子级.
2.当表格B出现时,我希望表格A关闭.

我曾尝试过但无法解决此问题,此问题与线程有关吗?
请帮忙!

There are two forms

Form A ( Parent )
Form B ( Child )

1. The Problem is to make Form B as Parent and Form A as Child when Form B shows.
2. When Form B shows up i want Form A to close.

i haved tried but unable to fix this problem, has this problem got anything to do with threading?
Please help!

推荐答案

即使类System.Windows.Forms.Form具有属性System.Windows.Forms.Control.ControlsSystem.Windows.Forms.Control.Parent,表单之间的父子关系也不是.确实可以操作.

(更确切地说,如果您尝试使一种形式成为另一种形式的父项,则会引发异常.您可以通过为子表单分配属性TopLevel = false并将另一种表单作为其父项来避免这种情况,但是结果很丑陋:一种形式位于另一种形式的客户区域内作为控件.您永远都不要这样做.)

因此,由于上一段中所述的原因,忘记了表单之间的子母关系,却再也记不清了..

现在,这与线程无关.此外,您只能从同一UI线程调用UI的任何方法/属性.要从其他线程访问UI,您需要使用UI线程调用,但这是一个单独的主题.有趣的是,您可以在不同的线程中运行两个或多个独立的UI,但这是一个高级主题,几乎不建议这样做.暂时不要忘记线程.

现在,您要使用表单的方式确实很糟糕.您应该记住UI中的一种表单是主表单.它由Application.Run中使用的参数定义(通常,此代码放置在应用程序输入方法中,通常为Main).如果关闭主窗体,则应用程序退出.您可以将其与其他形式的运行应用程序一起使用.您可以使用这个事实.

以称为PromoteToMainForm
的主要形式创建一些方法
Even though the class System.Windows.Forms.Form has the properties System.Windows.Forms.Control.Controls and System.Windows.Forms.Control.Parent, the parent-child relationship between forms is not really operational.

(More exactly, if you try to make one form a parent of another, an exception will be thrown. You can avoid it by assigning the property TopLevel = false for a child form and make another form its parent, but the result is ugly: one form sitting inside the client area of another form as a control. You should never do it.)

So, by the reason explained in the previous paragraph, forget about child-parent relationship between forms and never remember it.

Now, this has nothing to do with threading. Moreover, you can only call any methods/properties of the UI from the same UI thread. To access UI from other threads you need to use UI thread invocation, but this is a separate topic. What is funny though — you can run two or more independent UIs in different threads, but this is an advanced topic and hardly can be advisable. For this moment, forget threading.

Now, what you want to do with forms is really bad style. You should remember one form in the UI is the main form. It''s defined by what parameter was used in Application.Run (normally, this code is placed in your application entry method, normally Main). If you close a main form, the application exits. You can use it running application with other form. You can use this fact.

Create some method in the main form called PromoteToMainForm
class FormMain : Form {
    void PromoteToMainForm() {
        PromotedAsMainForm = new //...
        Application.Exit(); //important, it won't terminate your process, see Main below
    } 
    internal Form PromotedAsMainForm;
}



然后,您的Main可以使用它:



Then, your Main can use it:

static void Main() {
    //...
    FormMain firstMain = new FormMain();
    Application.Run(firstMain);
    Form secondMain = firstMain.PromotedAsMainForm;
    if (secondMain != null)
        Application.Run(secondMain);
}



或者,隐藏主窗体而不是将其关闭.确保让用户有机会退出应用程序,例如,通过所有形式的菜单.



您真正需要使用的是表单之间的另一种关系:所有者/拥有的表单.如果您确实需要使用多种表单(但请参见下文),请帮自己一个忙:将所有表单归主表单所有.它将支持您的应用程序完整性:当您激活任何表单时,您的应用程序的所有表单都将以某些Z顺序排在顶部,就像所有窗口都将被Z分组一样,而在任何窗口之间都不会以Z顺序放置任何其他窗口您的申请表格的形式.为了获得类似的好处,请始终为非主要形式设置ShowInTaskbar = false.

参见:
http://msdn.microsoft.com/en-us/library/system. windows.forms.form.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.addownedform.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showintaskbar.aspx [ ^ ].

[END EDIT]

现在,一个真正的建议:如果真的需要,请仔细考虑.即使可以在某些情况下使用这种模式(感谢您的笔记,鲍勃),它们确实很少见.在大多数现实情况下,您应该做一些与众不同的事情.甚至不使用多种形式,仅使用一种主要形式.向其中添加一些容器控件并显示/隐藏它们,以使您的表单看起来不同,使用选项卡控件等.有很多不错的设计.

—SA



Alternatively, hide main form instead closing it. Make sure you leave the user the opportunity to exit the application, for example, via a menu of all forms.



What you really need to use, is another relationship between forms: Owner/Owned form. If you really need to use multiple forms (but see below), do yourself a big favor: make all forms owned by the main form. It will support your application integrity: when you activate any form, all the forms of your application will go on top certain Z-order the way that all the windows will be Z-grouped, without any other window placed in Z-order between any of the forms of your application. For the similar benefit, always set ShowInTaskbar = false for non-main forms.

See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.addownedform.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showintaskbar.aspx[^].

[END EDIT]

Now, a real advice: think thoroughly if you really need this. Even though such schema can be used in certain cases (thank you for your notes, Bob), they are really rare. In most real-life situations, you should do something really different. Don''t even use multiple forms, use only one, main form. Add some container control(s) to is and show/hide them to make your form look different, use tab control, etc. There are many good designs.

—SA


FormA应该是您的主要形式(在Program.cs
中提到的形式) Application.Run(new YourMainFormNameHere()); ),您仍然可以关闭它.但这也会关闭所有其他形式.
尝试隐藏FormA而不是将其关闭.
或者将一个完全不同的Form用作您的主要表单(不是FormA,不是FormB).然后,您可以无限制地关闭和重新打开FormA和FormB的实例.d
Should FormA be your main form (the one mentioned in Program.cs
Application.Run(new YourMainFormNameHere()); ), you can still close it. But that will close all other forms, too.
Try hiding FormA instead of closing it.
Or make a totally different Form your main form (not FormA, not FormB). Then you can close and re-open instances of FormA and FormB without restrictions.d


这篇关于亲子形式关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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