C# 子窗体关闭的同时父窗体也随之关闭

子窗体有个按钮 点击后子窗体关闭 父窗体也关闭
2024-12-02 10:35:21
推荐回答(4个)
回答1:

父窗体:form2.ShowDialog(this);
子窗体的Closed事件枣团里写:this.Owner.Close();
两个窗体凳陪橘就一起关了。乱备

回答2:

仅拦兆供参考:
1、父卜衡尘窗体Form1中调用子窗体Form2代码:
Form2 fr = new Form2();
fr.ShowDialog(this);

2、子窗体型禅的FormClosed事件代码:
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Form1 fr = new Form1();
fr = (Form1)this.Owner;
fr.Dispose();
fr.Close();
}

回答3:

子窗体关闭的方法里NEW一下父窗体,然后调用.close()

回答4:

form main
private void Form1_Load(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
f.FormClosed += Close;
}
private void Close(object sender, EventArgs e) //方法自定义,通过订阅委托执行
{
this.Close();
}
form child
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}