简体   繁体   中英

close MDI child form from another form

i have 3 forms.

  • main_frm is MDI
  • app_frm is child MDI
  • progress_frm just a form that shows progress of app_frm

in the progress_frm form i have a button named "cancel" that closes the progress_frm form. Then have the following event on closing the 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. 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.

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?

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 :( :

      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?? i tried changing loop to "currentForm.MdiChildren" and still got same null reference exception...

i thought i understood MDI concept, but now am getting confused on how to be able to reference them properly

Are you sure that your app_frm_temp object refers to the opened instance of app_frm Form? 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?

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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