简体   繁体   English

从另一个表单关闭MDI子表单

[英]close MDI child form from another form

i have 3 forms. 我有3种形式。

  • main_frm is MDI main_frm是MDI
  • app_frm is child MDI app_frm是子MDI
  • progress_frm just a form that shows progress of app_frm progress_frm只是一个显示app_frm进度的表格

in the progress_frm form i have a button named "cancel" that closes the progress_frm form. 在progress_frm表单中,我有一个名为“取消”的按钮,用于关闭progress_frm表单。 Then have the following event on closing the progress_frm. 然后在关闭progress_frm时发生以下事件。

    private void frm_progress_Closing(object sender, FormClosingEventHandler e)
    {

        Form currentForm = Form.ActiveForm;

        Form app_frm_temp = currentForm.ActiveMdiChild;

        app_frm_temp.Dispose();

    }

I am expecting that the form app_frm will close and terminate anything it was doing. 我期望app_frm表单将关闭并终止其正在执行的任何操作。 but that does not happen.. only the progress_frm form closes, and i still see app_frm running with the hour glass and still running it's process/thread. 但这不会发生..只有progress_frm窗体关闭,并且我仍然看到app_frm与沙漏一起运行,并且仍在运行它的进程/线程。

My goal is that if a user wants to abort and close the process that app_frm started, they would be able to terminate and close app_frm from progress_frm? 我的目标是,如果用户要中止并关闭app_frm启动的进程,他们将能够从progress_frm终止并关闭app_frm?

after the feedback below i tried the following, my form was not hitting the closing event because i copied and pasted it from another form , i then went on the design portion of progress_frm and made an event sorry for confusiong on that :( : 在下面的反馈之后,我尝试了以下操作,我的表单没有遇到关闭事件,因为我从另一个表单复制并粘贴了该表单,然后继续进行progress_frm的设计部分,并为此感到困惑:(:

      private void progress_frm_FormClosing(object sender, FormClosingEventArgs e)
    {
        Form currentForm = Form.ActiveForm;



        foreach (Form frm in currentForm.MdiParent.MdiChildren) 
        { 
            if (frm.GetType() == currentForm.GetType()) 
            { 
                frm.Focus(); 
                return; 
            } 
        } 
    }

i get a null exception "object reference not set to an instance of an object" when the loop accesses currentForm.. remember my i am the progress_frm which is not part of the MDI config... i am trying to reference and close/terminate the child form app_frm whose parent is main_frm... i know that currentForm is main_frm, but not sure why it will not find the child form so i can reference it?? 当循环访问currentForm时,我得到一个空异常“对象引用未设置为对象的实例”。.记住我我是不是MDI配置一部分的progress_frm ...我正在尝试引用和关闭/终止子窗体app_frm的父窗体是main_frm ...我知道currentForm是main_frm,但是不确定为什么它找不到子窗体,所以我可以引用它? i tried changing loop to "currentForm.MdiChildren" and still got same null reference exception... 我尝试将循环更改为“ currentForm.MdiChildren”,但仍然遇到相同的空引用异常...

i thought i understood MDI concept, but now am getting confused on how to be able to reference them properly 我以为我了解MDI概念,但现在对如何正确引用它们感到困惑

Are you sure that your app_frm_temp object refers to the opened instance of app_frm Form? 您确定您的app_frm_temp对象引用了app_frm表单的打开实例吗? If it is then on FormClosing event of your app_frm you have to properly send the closing notification to your process/thread, a nice example is given here for stopping the background thread/process before closing the form: How to stop BackgroundWorker on Form's Closing event? 如果随后在FormClosing事件上发生,则必须正确地向您的进程/线程发送关闭通知,此处给出了一个很好的示例,用于在关闭表单之前停止后台线程/进程: 如何在Form的Closing事件上停止BackgroundWorker ?

But before this just to make sure you are refering to correct instance of Form, this is how you can loop through all opened MDI childs and get the reference to one you are interested in: 但是在此之前,只是要确保您引用的是正确的Form实例,这是如何遍历所有打开的MDI子级并获取对您感兴趣的子级的引用的方式:

foreach (Form frm in this.MdiParent.MdiChildren)
     {
          if (frm.GetType() == app_frm.GetType())
          {
               frm.Focus();
               return;
          }
     }

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

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