C#关闭子窗口同时关闭父窗口进程

方法一
只需把以下代码复制到子窗口即可(或者写在子窗口的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);

posted on 2012-06-20 17:39  张世民  阅读(4222)  评论(0编辑  收藏  举报