C# 父子关系中的 MDI 表单
我的项目有 3 个表格。
form1
是 MDI 控制器form2
和form3
是 MDI 子级
如何创建 form1
作为父级和 form2
和 form3
作为子项?
类似于旧 MFC 的 MDI 界面:
假设 form2
是父级并且有一个按钮。如果单击,则必须在父表单 (form1
) 中打开 form3
。我该如何设置?
I have 3 forms on my project.
form1
is MDI controllerform2
andform3
are MDI children
How do I create form1
as the parent and form2
and form3
as children?
Something like old MFC's MDI interface:
Imagine form2
is the parent and has a button. If clicked, it must open form3
in the parent form (form1
). How do I set this up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,确保 Form1 的
IsMdiContainer
设置为true
。然后实例化 Form1 和 Form2,将 Form1 设置为 Form2 的 MdiParent:
在 Form2 的代码中,使用类似以下内容来处理按钮的单击事件以实例化 Form3。
需要注意的是,这段代码确实又快又脏。它有一些不受欢迎的事情,例如 Form2 和 Form3 的耦合(以及无用的类名 Form1、Form2 和 Form3)。理想情况下,您可以通过从 Form2 引发一个事件来解耦 Form2 和 Form3,您的表单容器可以挂接到该事件并实例化 Form3。此示例代码旨在为您提供指导。
Firstly, make sure Form1's
IsMdiContainer
is set totrue
.Then instantiate Form1 and Form2, setting Form1 to be Form2's MdiParent:
In Form2's code, have something like the following to handle the button's click event to instantiate Form3.
As a couple notes, this code is really quick and dirty. There are several undesirable things about it like the coupling of Form2 and Form3 (as well as unhelpful class names Form1, Form2, and Form3). Ideally, you'd decouple Form2 and Form3 by raising an event from Form2 that your forms container can hook onto and instantiate Form3. This sample code is meant to give you a direction.
只需告诉表单其 MdiParent 是当前表单即可。
Just tell to the form that its MdiParent is current form.
然后在您调用的表单中
Then in the Form you are calling
试试这个功能
try this function