简体   繁体   English

如何从另一个 MDI 子窗体打开 MDI 子窗体

[英]how to open a MDI child form from another MDI child form

I have a form with the property "MDI container" set to true that opens MDI children when pressing Labels on a MenuStrip, but I have two problems:我有一个将属性“MDI 容器”设置为 true 的表单,当按下 MenuStrip 上的标签时会打开 MDI 子项,但我有两个问题:

The first one is that once I open an MDI Child I can not open another one ;第一个是,一旦我打开一个 MDI Child,我就不能再打开另一个 I press different labels on the same MenuStrip that I pressed to open the current MDI child and nothing happens.我在打开当前 MDI 子项时按下的同一个 MenuStrip 上按下了不同的标签,但没有任何反应。

The second problem is that I can not open an MDI child form from another MDI child form from code .第二个问题是我无法从代码中打开另一个 MDI 子窗体的 MDI 子窗体。

Following this paragraph, I will show the relevant parts of my code and some things that I have tried (with no solutions)在本段之后,我将展示我的代码的相关部分以及我尝试过的一些事情(没有解决方案)

//Event of the MenuStrip that opens an MDI child (homePage or sellProduct) from the MDI container

HomePage homePage = null;
SellProduct sellProduct = null;

private void HomeToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (homePage == null)
    {
        homePage = new HomePage();
        homePage.TopLevel = false;
        homePage.MdiParent = this;
    }

    homePage.Show();
}

private void ToolStripSellPtoduct_click(object sender, EventArgs e)
{
    if (sellProduct == null)
    {
        sellProduct = new SellProduct();
        sellProduct.TopLevel = false;
        sellProduct.MdiParent = this;
    }

    sellProduct.Show();
}

I have tried to copy this in a child form but it does not work.我试图以子形式复制它,但它不起作用。 something that may be important is that when I load the MDI container I also load the first MDI child :可能重要的是,当我加载 MDI 容器时,我还加载了第一个 MDI 子项

private void MainPage_Load(object sender, EventArgs e)
{
    if (homePage == null)
    {
        homePage = new HomePage();
        homePage.TopLevel = false;
        homePage.MdiParent = this;
    }
    homePage.Show();
}

And that is all the code I consider necesary for the first problem (I can not open a MDI child form from another using my MenuStrip).这就是我认为第一个问题所需的所有代码(我无法使用我的 MenuStrip 从另一个表单打开 MDI 子表单)。 If you need anything from my code I will provide it.如果您需要我的代码中的任何内容,我会提供。

In the second problem (I can not open an MDI child form from another from code) I am trying to open the MDI child form "HomePage" from the other one "SellProduct" when pressing a button located in the last one:在第二个问题中(我无法从代码中打开另一个 MDI 子表单),当按下最后一个按钮时,我试图从另一个“SellProduct”打开 MDI 子表单“HomePage”:

public partial class SellProduct : Form
{
    public SellProduct()
    {
        InitializeComponent();
        }

            private void Button_Sale_Click(object sender, EventArgs e)
            {
                HomePage homePage = new HomePage();
                homePage.show();
                this.close();
            }
        }
    }
}

The code above closes MDI form SellProduct showing the mdiparent (but it does not execute again the mdi parent, and the MenuStrip still does not work, is weird) and opens a MDI parent (where the MenuStrip actually works).上面的代码关闭了显示 mdiparent 的 MDI 表单 SellProduct(但它不会再次执行 mdi 父级,而且 MenuStrip 仍然不起作用,很奇怪)并打开一个 MDI 父级(MenuStrip 实际工作的地方)。 So no, it does not open another mdi child, it just do weird stuffs.所以不,它不会打开另一个 mdi 孩子,它只会做一些奇怪的事情。

And that is all, thank you for your time, every help is welcome, and hope you have a great day (: .仅此而已,感谢您的宝贵时间,欢迎您提供任何帮助,并希望您度过愉快的一天(:.

I have finally solved this, this is my solution:我终于解决了这个问题,这是我的解决方案:

Problem 1) Once I open an MDI Child I can not open another one;问题 1)一旦我打开一个 MDI Child,我就不能再打开另一个; I press different labels on the same MenuStrip that I pressed to open the current MDI child and nothing happens.我在打开当前 MDI 子项时按下的同一个 MenuStrip 上按下了不同的标签,但没有任何反应。

Solution: The MDI child forms were not showing because I had to hide the one opened (from the MainPage) before showing another one.解决方案: MDI 子 forms 没有显示,因为我必须隐藏打开的一个(从 MainPage 中),然后再显示另一个。

Problem 2) I can not open an MDI child form from another MDI child form from code.问题 2)我无法从代码中打开另一个 MDI 子窗体的 MDI 子窗体。

Solution: It is the same problem as the first one If the actual form displayed is not hidden a new one can not be shown, because of that you have to hide the current one, and then open the new one:解决方法:和第一个一样的问题 如果实际显示的表格没有隐藏,新的不能显示,因为你必须隐藏当前的,然后打开新的:

//In this case I want to show the HomePage
this.Hide();
HomePage homePage = new HomePage();
homePage.Show();

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

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