简体   繁体   English

指定父MDI表单

[英]specify parent MDI form

I am trying to specify the parent MDI form when showing a form in c# 我正在尝试在C#中显示表单时指定父MDI表单

All the examples suggest just using 所有示例都建议仅使用

FormVariable.Parent = this;

this works ok assuming you want the form to be opened from the parent window all the time. 假设您希望一直从父窗口打开表单,则此方法正常。

I want to be able to open a form and set the Parent form to my MDI Parent form by specifying the name. 我希望能够打开一个表单,并通过指定名称将父表单设置为我的MDI父表单。

in VB.net I have used 在VB.net中,我已经使用过

Me.MdiParent = TheNameOfMyParentForm

When I try anything similar in c# 当我在C#中尝试类似的操作时

this.MdiParent = CruxMDI();

I get 我懂了

'Crux.CruxMDI' is a 'type' but is used like a 'variable' “ Crux.CruxMDI”是“类型”,但像“变量”一样使用

Form.MdiParent has to reference a concrete instance. Form.MdiParent必须引用一个具体实例。 So, maybe it would be a good idea to implement a Singleton pattern (you probably don't want to allow multiple parent windows either way, do you?) in your Parent container, so that you can reference it from wherever you need to. 因此,在父容器中实现Singleton模式(您可能不想以任何一种方式允许多个父窗口)是一个好主意,以便您可以从任何需要的地方引用它。 Then you'll just type: 然后,您只需输入:

this.MdiParent = CruxMDI.Instance;

If you want to add such behavior automatically and it needs to happen in many Forms in your application, you may consider an option when you'd create a custom base class inheriting from Form. 如果要自动添加此类行为,并且需要在应用程序中的许多Forms中发生,那么在创建从Form继承的自定义基类时,可以考虑使用一个选项。 That way you specify this once and then you just need to be sure to inherit your new Forms from this baseclass instead of a default Form. 这样,一次指定一次,然后只需要确保从该基类继承新的Forms,而不是默认的Form。

Either way, you need to have some kind of mechanism to reference the instance of your MDI container. 无论哪种方式,您都需要某种机制来引用MDI容器的实例。

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

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