c implements a simple example of child window closing and parent window closing

  • 2021-12-05 07:04:19
  • OfStack

In fact, it is a problem of communication between windows. Turn form2 on form1 and turn form1 off when form2 is turned off

Implementation method:

Declare events in child window form2:


public delegate void childclose();
  public event childclose closefather;

   This event is then triggered in its shutdown event: 

   private void Form2_Closed(object sender, System.EventArgs e)
   {
    // Close the main window with events 
    closefather();
   }

In the parent window form1 (such as the login window):

Then the place where the subform2 form pops up reads as follows:


  Form2 ff=new Form2();
  ff.closefather+=new childclose(this.closethis); //closethis() Is in the parent form 1 Methods 
  ff.Show();

   public void closethis()
   {
     this.Close();
   }


Related articles: