如何从打开中停止多个mdi表单实例 [英] How to stop multiple instances of mdi form from opening

查看:81
本文介绍了如何从打开中停止多个mdi表单实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MDI父表单,它包含我的mdi子表单,我想要实现的是停止打开子表单的多个实例。到目前为止,我已经能够实现这一点,即当子表单实例打开时,子表单的另一个实例无法打开。我现在的问题是,当我关闭子窗体时,它不会再次打开,直到我重新启动应用程序。下面是我的代码到目前为止的代码段。



I have an MDI parent form that holds my mdi child forms and what I am trying to achieve is to stop multiple instances of the children form from opening. So far I have been able to achieve this i.e when a child form instance is open, another instance of the child form cannot be open. The problem I have now is, when i close the child form, it does not open again until i restart the application. Below is a snippet from my code so far.

public partial class DataMigrationMDI : Form
   {

       ConnectToSource src_mdi_child = null;

       public DataMigrationMDI()
       {
           InitializeComponent();

       }

        private void connectToSourceDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
       {
           if (src_mdi_child == null)
           {
               src_mdi_child = new ConnectToSource();
               src_mdi_child.MdiParent = this;
               src_mdi_child.Show();
           }

       }

   }



所以,基本上我最初将子表单实例设置为null然后在工具条的onclick事件中,我检查子表单是否为空,然后打开它。但是使用此代码时,它不会检查子窗体何时关闭以将其重新分配回null,以便以这种方式重新打开子窗体。因此,我需要的帮助是显示如何检查子窗体何时关闭,然后将其分配回null。在此先感谢


So,basically I initially set the child form instance to null and then at the onclick event of the toolstrip i check if the child form is null and then opens it. But with this code it doesn't check when the child form is closed to re-assign it back to null so that in this way the child form can be re-opened. So the help I need is to be shown how to check when a child form has been closed and then assign it back to null. Thanks in advance

推荐答案

Hello Uzoma



在代码打开任何孩子之前调用此函数,

Hello Uzoma

Call this function before the code opening the any child,
private void CloseMdichilds()
        {
            this.MdiParent.MdiChildren.OfType<Form>().ToList().ForEach(x => x.Close());
        }


我建​​议阅读谢尔盖关于使用MDI的答案 [ ^ ]。



I would recommend to read Sergey's answers about using MDI[^].

Sergey Alexandrovich Kryukov写道:
Sergey Alexandrovich Kryukov wrote:

这是使用MDI孩子的最佳方式:永远不要使用MDI。



这是一个想法:谁需要MDI?为什么折磨自己并吓跑用户?

Here is the best way of using MDI child: never using MDI.

Here is the idea: who needs MDI, ever? Why torturing yourself and scaring off your users?


class ParentForm : Form {
    frmWebLeads formWeblead = null;

    //...

    private void leadsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if(formWeblead != null) return;
        formWeblead = new frmWebLeads();
        formWeblead.MdiParent = this;
        formWeblead.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        formWeblead.Show();

    }

}


这篇关于如何从打开中停止多个mdi表单实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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