方法一
只需把以下代码复制到子窗口即可(或者写在子窗口的OnClosing事件中)

protected override void OnClosing(CancelEventArgs e)
{
  base.OnClosing(e);
  Application.Exit(e);
}

 


方法二:

在子窗口中声明事件:

public event childclose closefather;

然后在它的关闭事件中触发本事件:

private void Form1_Closed(object sender, System.EventArgs e)
{
  //用事件去关闭主窗口
  closefather();
}

 

在父窗口中(登陆窗口中):


在窗口类之前,在共同的命名空间之后声明代理:

public delegate void childclose();

然后弹出子窗体的地方这样写:

Form1 ff=new Form1();
ff.closefather+=new childclose(this.closethis); //closethis()是父窗体中的一个方法
ff.Show();

定义方法:

public void closethis()
{
this.Close();
}————————————————发现这么添加,系统不会自动在Form2.Designer.cs文件中加入事件关联,于是手动加入(Form2是我程序中的子窗口)this.Closed += new System.EventHandler(this.Form2_Closed);

相关文章:

  • 2022-12-23
  • 2021-10-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-11
  • 2021-12-13
  • 2022-12-23
猜你喜欢
  • 2021-08-22
  • 2022-02-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-07
相关资源
相似解决方案