如何在 C# 中修复 "Change MdiParent in MdiChild"

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

我在 MdiParent 中更改 MdiChild 的代码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;
        }

它在 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();
        }

但我从 child 调用了 SetupMdi 方法,但它的工作方式 child 没有显示,但它使用 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();
    }
}

我尝试了一个 ApiClass,它不像 child class

那样工作

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

假设您的代码在实际的 MdiChild 中,那么您可以将 MdiParent 属性 转换为您的父类型(我相信,Menu?),然后调用 SetupMdi()方法:

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