简体   繁体   English

如何从MDIParent表单调用MdiChild

[英]how to call MdiChild from MDIParent form

I create a new MdiChild from MainForm using this method: 我使用以下方法从MainForm创建一个新的MdiChild:

AdminLogInForm adminForm;
 private void LogInAsAdminMenuItem_Click(object sender, EventArgs e)
    {
        if (adminForm == null)
        {
            adminForm = new AdminLogInForm();
            adminForm.MdiParent = this;
            adminForm.Show();
            adminForm.Dock = DockStyle.Fill;
            adminForm.BringToFront();
            LogInAsAdminMenuItem.Enabled = false;              
        }
        else
        {
            adminForm.Activate();
            adminForm.BringToFront();
        }
    }

Why when i close my child, using in chld form "this.close()" using that method i cant open it anymore? 为什么当我关闭孩子时,使用chld形式的“ this.close()”使用该方法无法打开它?

there i call close(); 我在那叫close();

        private void cancelLogInButton_Click(object sender, EventArgs e)
    {
        this.MdiParent.Activate();            
        if(this.MdiParent!=null)
        ((MainForm)this.MdiParent).LogInAsAdminMenuItem.Enabled = true;
        this.Close();
    }

by the way to make work that I asked before I hed to plase this.Close(); 顺便做一下我在顺手讨好这个之前要求的工作。Close(); after all statements . 毕竟声明。

By closing the form you are not making adminForm instance to null (Which is what your if condition will check when you try to open it next time.) 通过关闭表单,您不会使adminForm实例为null(这是您的if条件在您下次尝试打开它时将检查的内容。)

On diposal of your form make adminForm = null and then your if condition will work next time. adminForm表单时,使adminForm = null ,然后if条件将在下次运行。

private void LogInAsAdminMenuItem_Click(object sender, EventArgs e)
    {
        if (adminForm == null)
        {
            adminForm = new AdminLogInForm(this);
            adminForm.Disposed += new EventHandler(adminForm_Disposed); //Add Disposed EventHandler
            adminForm.MdiParent = this;
            adminForm.Show();
            adminForm.Dock = DockStyle.Fill;
            adminForm.BringToFront();
            LogInAsAdminMenuItem.Enabled = false;              
        }
        else
        {
            adminForm.Activate();
            adminForm.BringToFront();
        }
    }

    void adminForm_Disposed(object sender, EventArgs e)
    {
        adminForm = null;
    }

As Described by Marshal that the closing of a form makes it disposed you should add a condition for disposing as well like this 正如元帅所描述的,表格的关闭使其可以处置,您也应该像这样添加处置条件

AdminLogInForm adminForm;

private void LogInAsAdminMenuItem_Click(object sender, EventArgs e)
    {
        if (adminForm == null || adminForm.IsDisposed)
        {
            adminForm = new AdminLogInForm();
            adminForm.MdiParent = this;
            adminForm.Show();
            adminForm.Dock = DockStyle.Fill;
            adminForm.BringToFront();
            LogInAsAdminMenuItem.Enabled = false;              
        }
        else
        {
            adminForm.Activate();
            adminForm.BringToFront();
        }
    }

Or you can also create a function to use a form as mdi like this 或者您也可以创建一个函数来使用像mdi 这样的表单

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

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