none
Forms Dispose from parent, will it dispose child? RRS feed

  • Question

  • Hello,
         I do not understand how this works.  I may be unsure of what the true definitions of parent and child forms are.  I think a parent child relationships is made like this


    class Form: Form1 
       private Form2 some_form; 
     
       private void button1_Click(object sender, EventArgs e) 
       { 
          some_form = new Form2(); 
          some_form.Show(); 
          this.hide(); 
        } 
     
     


    If I am correct, when I dispose of a Form1 object, I do not see Form2 being disposed.  Is there some way that I am supposed to create this relationship?
    Monday, January 19, 2009 10:15 PM

Answers

  •  It means I was wrong. I did no tthink clearly but now I do understand that it sould be the case. I sort of suspected I was wrong when I posted that.

    From your entire discourse I faile dto see you ultimate goal. What do you want to create? Do you want the child form to be killed when you dispose of the main form? You can either close the child form programmatically from the parent form or you can kill the process for the child while the parent is still alive. System.Diagnostics.Process. Find the window handle you need and kill it.

    AlexB
    • Proposed as answer by Harry Zhu Wednesday, January 21, 2009 1:11 PM
    • Marked as answer by Harry Zhu Friday, January 23, 2009 4:15 AM
    Tuesday, January 20, 2009 1:58 PM
  • Types that own disposable fields should be disposable and dispose all disposible fields in its IDisposable implementation. If you are compiling your code under Visual Studio Team Suit your code will be flagged with warning CA1001

    MSMVP VC++
    • Proposed as answer by Harry Zhu Wednesday, January 21, 2009 1:10 PM
    • Marked as answer by Harry Zhu Friday, January 23, 2009 4:15 AM
    Wednesday, January 21, 2009 12:23 AM

All replies

  •  Your Form2 is a child and Form is parent. If you kill (dispose of) form1 you will do away with form2 as well as there is no way for form2 to exist on its own, right?
    AlexB
    Monday, January 19, 2009 10:21 PM
  • I have the compact framework and this is not what I am seeing.  I have 3 forms, each makes a child form with the syntax above.  They are form1, form2, and form3.  If I dispose of Form2, form3 is still visible...


    I don't understand why...

    Is this unique to the compact framework??
    Monday, January 19, 2009 10:25 PM
  • I tried the same code in regular visual studio and I get the same result.  Form1 and form3 sustain

    Please, any help on this would be great.  Am I supposed to customize the disposed method?
    Monday, January 19, 2009 11:06 PM
  •  It means I was wrong. I did no tthink clearly but now I do understand that it sould be the case. I sort of suspected I was wrong when I posted that.

    From your entire discourse I faile dto see you ultimate goal. What do you want to create? Do you want the child form to be killed when you dispose of the main form? You can either close the child form programmatically from the parent form or you can kill the process for the child while the parent is still alive. System.Diagnostics.Process. Find the window handle you need and kill it.

    AlexB
    • Proposed as answer by Harry Zhu Wednesday, January 21, 2009 1:11 PM
    • Marked as answer by Harry Zhu Friday, January 23, 2009 4:15 AM
    Tuesday, January 20, 2009 1:58 PM
  • Types that own disposable fields should be disposable and dispose all disposible fields in its IDisposable implementation. If you are compiling your code under Visual Studio Team Suit your code will be flagged with warning CA1001

    MSMVP VC++
    • Proposed as answer by Harry Zhu Wednesday, January 21, 2009 1:10 PM
    • Marked as answer by Harry Zhu Friday, January 23, 2009 4:15 AM
    Wednesday, January 21, 2009 12:23 AM
  • The relationship needs to be handled via Form.AddOwnedForm Method.

    MSDN Topic: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.addownedform.aspx

    Child forms will be closed properly with parent.

    Handle dispose functionality in OnClosed/OnClosing methods.


    Wednesday, February 20, 2013 8:50 PM