1 instantiating a child window in the main window
Form2 f2 = new Form2();
2 displaying child windows by buttons
f2.Show();
3 Ways to close child windows without releasing child window objects
protected override void Dispose(bool disposing) { Hide(); //if (disposing && (components != null)) //{ // components.Dispose(); //} //base.Dispose(disposing); }
(Note: Modified in the child window designer file)
4 Destroying a child window object when the parent window is closed
The closing function added in the child window class is as follows:
public void Close() { this.Dispose(); }
The parent window close is to destroy the child window object, so in the parent window's Close action formclosed Add the Destroy function that calls the Subwindow F2.
private void Form1_FormClosed(object sender, FormClosedEventArgs e) { f2.Close(); }
===================================================================================
There is another way:
A subform defines a delegate that invokes the event method when the subform is closed when the parent form instantiates the subform when the subform's delegate event is registered (specifically, the control that implements the parent window changes).
Parent form
public void Showpopwindow ()
{
TestA sub = new TestA ();
Sub. CloseWindow + = new Action (Sub_closewindow);
Sub.show ();
}
void Sub_closewindow ()
{
Change the contents of a parent form control
}
Child form
public class TestA
{
public event Action CloseWindow;
protected virtual void Onclosewindow ()
{
Action handler = CloseWindow;
if (handler! = NULL) handler ();
}
Subform Close Event
private void Closesubwindow ()
{
Onclosewindow ();
}
}
Source: https://zhidao.baidu.com/question/134623114688436765.html
========================================================================
C # Close Subform Release Subform Object problem