C # Close Subform Release Subform Object problem

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.