0

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

thanosazlin
  • 189
  • 1
  • 10
  • if both frm_progress and app_frm are child of same Mdi parent then i don't think so there is any need of using Form.ActiveForm; Instead in foreach loop, use this.MdiParent.MdiChildren... And also, in order to close form replace 'frm.Focus()' with 'frm.Close()' – Waqas Aug 22 '11 at 01:59

1 Answers1

0

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;
          }
     }
Community
  • 1
  • 1
Waqas
  • 6,812
  • 2
  • 33
  • 50
  • thx , i updated my question... i tried what you suggested and still not getting anywhere.. i did reference progress_frm as frm_progress in the closing event, sorry i corrected that... – thanosazlin Aug 22 '11 at 01:55