使用三个 winform 作为 MDI 父级和子级
我有 3 个表格 - Form1、2、3。 Form1 是 MdiContainer。现在,当单击 Form1 上的按钮时,它会显示 Form2,而在 Form2 上单击按钮时,它会显示 Form3。我的代码如下,但它给出了 Form2 不是 MdiContainer 的错误。但如果我将 Form2 设置为 MdiContainer,则会出现错误,指出 Form2 是 MdiChild 并且不能是 MdiContainer。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
f2.MdiParent = this;
}
}
公共分部类 Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.Show();
f3.MdiParent = this;
}
}
如何做到这一点?
简而言之,我想要的是: Form2 的父级是 Form1,并且, Form3 的父级是 Form2
I have 3 forms-Form1,2,3. Form1 is MdiContainer. Now, when a button on Form1 is clicked it shows Form2, and on Form2 when a button is clicked then it shows Form3. my code is as below but it gives error that Form2 is not MdiContainer. but if i make Form2 as MdiContainer then it gives error that Form2 is a MdiChild and can't be MdiContainer.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
f2.MdiParent = this;
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.Show();
f3.MdiParent = this;
}
}
how to do this?
In short , i want that :
Parent of Form2 is Form1, and,
Parent of Form3 is Form2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你不能。您只能有一个 MDI 父级。在该父容器中,您可以有许多子窗体,但这些窗体只能直接生成其他窗体,而不能作为 MDI 子窗体。
用微软自己的话说:
You can't. You can only have one MDI parent. Within that parent container you can have many child forms, but those forms can only spawn other forms directly and not as MDI children.
In Microsoft's own words:
它不起作用,因为 Form2 不是 MDI 容器。
It won't work because Form2 is not an MDI container.
基本上不支持此功能,但是您可以通过其他方式复制它。您希望 f2 成为 f3 的父级仅仅是为了视觉表示吗?我试图理解以便给你一个正确的答案。那么你想实现什么目标呢?如果您希望将 f3 嵌入到 f2 上,您可以创建一个用户控件并将其作为控件添加到窗体中。
如果您希望 f3 弹出,您可以使用
如果这样做,请不要指定 f3 的 mdiparent。
埃里克
Basically this functionality is not supported, however you may be able to replicate it in other ways. Is it simply for visual representation that you want f2 to be the parent of f3? I am trying to understand in order to give you a proper answer. So what do you want to accomplish? If you want f3 to be embeded on f2, you can create a user control and add it as a control to your form.
If you want f3 to popup you could use
If you do this don't specify the mdiparent of f3.
Eric
我尝试在不使用 showdialog 的情况下编写如下
代码,
如果您想在不使用 showdialog 的情况下进行操作,请在 Button_click 上编写以下内容
I tried by writing as follows with out
The code works
If you want to do with out showdialog means write the following on Button_click
一个应用程序中只有一个MdiContainer,其他的应该是子窗体。
但是,您可以通过执行
ShowDialog
而不是Show()
编辑 1 来大致实现您的功能。根据您的意见,@Dorababu 最适合您,并进行了一些改进;将
form1
作为form3
的父级。因此,您的 form3 将显示在MdiParent
内。如果显示
form3
,则检查 form2 的 getfocus
事件,然后将焦点设置为form3
,这样在打开 form3 之前,form2 不会获得焦点。There is only one MdiContainer in an application and others should be child forms.
However you can approximately achieve your functionality by doing
ShowDialog
instead ofShow()
Edit 1 Due to your comments, Solution provided by @Dorababu is best for you with some improvements; made
form1
as parent ofform3
. So your form3 will be displayd insideMdiParent
.And check on get
focus
event of form2 ifform3
is displayed then set focus toform3
, so form2 will not get focused until form3 is opened.