简体   繁体   中英

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:

The first one is that once I open an MDI Child I can not open another one ; I press different labels on the same MenuStrip that I pressed to open the current MDI child and nothing happens.

The second problem is that I can not open an MDI child form from another MDI child form from code .

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 :

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). 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:

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). So no, it does not open another mdi child, it just do weird stuffs.

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; I press different labels on the same MenuStrip that I pressed to open the current MDI child and nothing happens.

Solution: The MDI child forms were not showing because I had to hide the one opened (from the MainPage) before showing another one.

Problem 2) I can not open an MDI child form from another MDI child form from code.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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