简体   繁体   English

如何使用C#单击MDI中的另一个子窗体以关闭上一个子窗体

[英]How to close the previous child form on clicking another child form in mdi using c#

I have a mdi parent form and I open my other forms in run time as mdi child form by this code: 我有一个MDI父表单,并通过以下代码在运行时以MDI子表单形式打开其他表单:

private void winAppToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Upload objWA = new Upload();
            objWA.MdiParent = this;
            objWA.Show();
            //objWA.WindowState = FormWindowState.Maximized;
        }

        private void userInfoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Reports objUI = new Reports();
            objUI.MdiParent = this;
            objUI.Show();
            //objUI.WindowState = FormWindowState.Maximized;
        }

but the problem is: When current form is open, user can open another form and it can be repeated several times so that each form is opened what's code for closing the previous child form before user open a new child form?? 但是问题是:当打开当前表单时,用户可以打开另一个表单,并且可以重复多次,以便打开每个表单,在用户打开新的子表单之前,关闭前一个子表单的代码是什么?

Screen shot for reference if we see the image my upload and reports forms both are getting opened on one another but it should only show the currently opened form 如果我们看到我上传的图像和报告表单都正在彼此打开的图像,以供参考,但应仅显示当前打开的表单

public void DisposeAllButThis(Form form){
foreach (Form frm in this.MdiChildren)
{
    if (frm.GetType() == form.GetType() 
        && frm != form)
    {
        frm.Dispose();
        frm.Close();
    }
}}

got this from: C# loop through all MDI childer and close all other forms except the current 从以下代码得到此信息: C#遍历所有MDI childer并关闭除当前表单以外的所有其他表单

private void winAppToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Upload objWA = new Upload();
        objWA.MdiParent = this;
        objWA.Show();
        //objWA.WindowState = FormWindowState.Maximized;
    }

    private void userInfoToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Reports objUI = new Reports();
        objUI.MdiParent = this;
        objUI.Show();
        DisposeAllButThis(Form objUI);
        //objUI.WindowState = FormWindowState.Maximized;
    }
    public void DisposeAllButThis(Form form)
    {
        foreach (Form frm in this.MdiChildren)
        {
            if (frm.GetType() == form.GetType() 
                && frm != form)
            {
                frm.Dispose();
                frm.Close();
            }
        }
    }

let me know if it worked because i just made it up 让我知道它是否有效,因为我刚刚做了

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

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