简体   繁体   English

作为mdi父母和孩子与三个winforms一起工作

[英]working with three winforms as mdi parent and child

I have 3 forms-Form1,2,3. 我有3个表格-Form1,2,3。 Form1 is MdiContainer. Form1是MdiContainer。 Now, when a button on Form1 is clicked it shows Form2, and on Form2 when a button is clicked then it shows Form3. 现在,当单击Form1上的按钮时,它显示Form2,而在Form2上,单击按钮时,它显示Form3。 my code is as below but it gives error that Form2 is not MdiContainer. 我的代码如下,但是它给出了Form2不是MdiContainer的错误。 but if i make Form2 as MdiContainer then it gives error that Form2 is a MdiChild and can't be 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;
    }
}

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 简而言之,我想要:Form2的父级是Form1,而Form3的父级是Form2

You can't. 你不能 You can only have one MDI parent. 您只能有一个MDI父级。 Within that parent container you can have many child forms, but those forms can only spawn other forms directly and not as MDI children. 在该父容器中,您可以有许多子窗体,但是这些窗体只能直接生成其他窗体,而不能作为MDI子窗体。

In Microsoft's own words: 用微软自己的话说:

The Microsoft Windows implementation of the multiple document interface (MDI) does not support nested MDI client windows. 多文档界面(MDI)的Microsoft Windows实现不支持嵌套的MDI客户端窗口。 In other words, neither an MDI client window nor an MDI child window can have an MDI client window as a child. 换句话说,MDI客户端窗口和MDI子窗口都不能将MDI客户端窗口作为子窗口。

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

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? 您是否想让f2成为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. 如果希望将f3嵌入在f2上,则可以创建一个用户控件并将其作为控件添加到表单中。

If you want f3 to popup you could use 如果您想弹出f3,可以使用

f3.showdialog();

If you do this don't specify the mdiparent of f3. 如果这样做,请不要指定f3的中间字母。

Eric 埃里克

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 如果要用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();
}

There is only one MdiContainer in an application and others should be child forms. 一个应用程序中只有一个MdiContainer,其他应为子窗体。

However you can approximately achieve your functionality by doing ShowDialog instead of Show() 但是,您可以通过执行ShowDialog而不是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; 编辑1根据您的评论, @ Dorababu提供的解决方案最适合您,并进行了一些改进。 made form1 as parent of form3 . 使form1作为form3form3 So your form3 will be displayd inside MdiParent . 因此,您的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();
}

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. 并检查是否显示了form3的form2的获取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;
        }
    }
}

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

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