使用三个 winform 作为 MDI 父级和子级

发布于 2024-10-25 22:29:15 字数 859 浏览 4 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风渺 2024-11-01 22:29:15

你不能。您只能有一个 MDI 父级。在该父容器中,您可以有许多子窗体,但这些窗体只能直接生成其他窗体,而不能作为 MDI 子窗体。

用微软自己的话说:

多文档界面 (MDI) 的 Microsoft Windows 实现不支持嵌套 MDI 客户端窗口。换句话说,MDI 客户端窗口和 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:

The Microsoft Windows implementation of the multiple document interface (MDI) does not support nested MDI client windows. In other words, neither an MDI client window nor an MDI child window can have an MDI client window as a child.

我喜欢麦丽素 2024-11-01 22:29:15

它不起作用,因为 Form2 不是 MDI 容器。

It won't work because Form2 is not an MDI container.

动次打次papapa 2024-11-01 22:29:15

基本上不支持此功能,但是您可以通过其他方式复制它。您希望 f2 成为 f3 的父级仅仅是为了视觉表示吗?我试图理解以便给你一个正确的答案。那么你想实现什么目标呢?如果您希望将 f3 嵌入到 f2 上,您可以创建一个用户控件并将其作为控件添加到窗体中。

如果您希望 f3 弹出,您可以使用

f3.showdialog();

如果这样做,请不要指定 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

f3.showdialog();

If you do this don't specify the mdiparent of f3.

Eric

留一抹残留的笑 2024-11-01 22:29:15

我尝试在不使用 showdialog 的情况下编写如下

 f2.MdiParent = this;

代码,

如果您想在不使用 showdialog 的情况下进行操作,请在 Button_click 上编写以下内容

bool IsOpen = false;
foreach (Form f in Application.OpenForms)
{
    if (f.Text == "Form2") //  Name of the Form
    {
        IsOpen = true;
        f.Focus();
        break;
    }
}
if (IsOpen == false)
{
    Form2 f2 = new Form2();
   //f2.MdiParent = this;
    f2.Show();
}

I tried by writing as follows with out

 f2.MdiParent = this;

The code works

If you want to do with out showdialog means write the following on Button_click

bool IsOpen = false;
foreach (Form f in Application.OpenForms)
{
    if (f.Text == "Form2") //  Name of the Form
    {
        IsOpen = true;
        f.Focus();
        break;
    }
}
if (IsOpen == false)
{
    Form2 f2 = new Form2();
   //f2.MdiParent = this;
    f2.Show();
}
不打扰别人 2024-11-01 22:29:15

一个应用程序中只有一个MdiContainer,其他的应该是子窗体。

但是,您可以通过执行 ShowDialog 而不是 Show()

private void button1_Click(object sender, EventArgs e)
{
    Form3 f3 = new Form3();
    f3.ShowDialog();
}

编辑 1 来大致实现您的功能。根据您的意见,@Dorababu 最适合您,并进行了一些改进;将 form1 作为 form3 的父级。因此,您的 form3 将显示在 MdiParent 内。

private void button1_Click(object sender, EventArgs e)
{
    Form3 f3 = new Form3();
   //f3.MdiParent = this;
    // instead use this.MdiParent
    f3.MdiParent = this.MdiParent;
    f2.Show();
}

如果显示 form3,则检查 form2 的 get focus 事件,然后将焦点设置为 form3,这样在打开 form3 之前,form2 不会获得焦点。

private void Form2_Activated(object sender, EventArgs e)
{
    // inside Focus event of Form2
    foreach (Form f in Application.OpenForms)
    {
        if (f.Text == "Form3") //  Name of the Form
        {
            f.Focus();
            break;
        }
    }
}

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 of Show()

private void button1_Click(object sender, EventArgs e)
{
    Form3 f3 = new Form3();
    f3.ShowDialog();
}

Edit 1 Due to your comments, Solution provided by @Dorababu is best for you with some improvements; made form1 as parent of form3. So your form3 will be displayd inside MdiParent.

private void button1_Click(object sender, EventArgs e)
{
    Form3 f3 = new Form3();
   //f3.MdiParent = this;
    // instead use this.MdiParent
    f3.MdiParent = this.MdiParent;
    f2.Show();
}

And check on get focus event of form2 if form3 is displayed then set focus to form3, so form2 will not get focused until form3 is opened.

private void Form2_Activated(object sender, EventArgs e)
{
    // inside Focus event of Form2
    foreach (Form f in Application.OpenForms)
    {
        if (f.Text == "Form3") //  Name of the Form
        {
            f.Focus();
            break;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文