下面小编就为大家带来一篇c# 实现子窗口关闭父窗口也关闭的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
其实是窗口间通讯的问题,在form1上打开form2 ,form2 关闭时关闭form1
实现方法:
在子窗口form2中声明事件:
public delegate void childclose();
public event childclose closefather;
然后在它的关闭事件中触发本事件:
private void form2_closed(object sender, system.eventargs e)
{
//用事件去关闭主窗口
closefather();
}
在父窗口form1中(比如登陆窗口中):
然后弹出子form2窗体的地方这样写:
form2 ff=new form2();
ff.closefather+=new childclose(this.closethis); //closethis()是父窗体中的一个方法
ff.show();
public void closethis()
{
this.close();
}
以上就是c#实现关闭子窗口和关闭父窗口的案例的详细内容。
