如何在c#窗口中打开其他子窗体时关闭所有子窗体 [英] how to close all child forms when opening other child forms in window form app in c#

查看:935
本文介绍了如何在c#窗口中打开其他子窗体时关闭所有子窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将许多子表单附加到父表单但是在这里我希望当我打开一个子表单时,其他打开的子表单应该在c#中关闭。请帮我解决这个问题。

i have attached many child form to a parent form but here i want when i open a child form other opened child form should be closed in c# pls help me out with this.

推荐答案

试试这将解决你的问题



try this it will solve your issue

if (ActiveMdiChild != null)
    ActiveMdiChild.Close();







Ex:



假设我有一个MDIParent表格,然后在菜单项的点击事件上有一些子表格代码如下






Ex:

suppose i have a MDIParent form and some child forms then on the click event of menu item the code will be as follows

private void testMenuToolStripMenuItem_Click(object sender, EventArgs e)
      {
          if (ActiveMdiChild != null)
             {
               ActiveMdiChild.Close();
               ChildForm2 ch2 = new ChildForm2();
               ch2 .MdiParent = this;
               ch2 .Show();
             }

      }   



如果你想在关闭它之前检查当前活动表格名称的名称,那么以下内容代码将帮助您:




and if u want to check out the name of the current Active form name before closing it then the following code will help you out :

private void testMenuToolStripMenuItem_Click(object sender, EventArgs e)
       {
           if (ActiveMdiChild != null)
           {

               if (ActiveMdiChild.GetType().Name != "ChildForm2")
               {
                   ActiveMdiChild.Close();
                   ChildForm2 ch2= new ChildForm2();
                   ch2.MdiParent = this;
                   ch2.Show();
               }
               else
               {
                   MessageBox.Show("Form is already open");
               }

           }
           else
           {
               ChildForm2 ch2= new ChildForm2();
               ch2.MdiParent = this;
               ch2.Show();
           }

       }


这篇关于如何在c#窗口中打开其他子窗体时关闭所有子窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆