Search code examples
c#winformsmdichild

How to fix "Change MdiParent in MdiChild" in C#


My codes to change MdiChild in MdiParent class

        public void SetupMdi(Form form)
        {
            clearMdi();
            activeMdiForms.Add(form);
            form.MdiParent = this;
            form.Show();
            form.Location = new Point(0, 0);
            foreach(Form forms in activeMdiForms)
            {
                MessageBox.Show(forms.ToString());
            }
            return;
        }

        public void clearMdi()
        {
            foreach(Form form in activeMdiForms)
            {
                form.Dispose();
            }
            activeMdiForms.Clear();
            return;
        }

It's working perfectly in parent class

        private void Menu_Load(object sender, EventArgs e)
        {
            VersionChecker ver = new VersionChecker();
            versionLbl.Text = "Depo Stok Programı Version " + earlySettings.version;
            SetupMdi(new Login());
            GCTimer.Start();
        }

But I called SetupMdi method from child form its working but child form not showing but it using ram

public partial class Login : Form
{
     public async void login()
    {
        earlySettings.usrName = obj.UserName;
        MainMenu form = new MainMenu();
        new Menu().SetupMdi(new MainMenuMdi());
        this.Dispose();
    }
}

I tried an ApiClass its not working like child class


Solution

  • But I called SetupMdi method from child form its working but child form not showing but it using ram

    Assuming your code is in an actual MdiChild, then you can cast the MdiParent property to your parent type (I believe, Menu?), and then call the SetupMdi() method:

    // ... assuming we are in an MdiChild ...
    ((Menu)this.MdiParent).SetupMdi(new MainMenuMdi());