简体   繁体   English

从子窗口关闭父窗口

[英]Close the parent window from child window

I put button on parent window to go to child window . 我在父窗口上放置按钮以转到子窗口。 for closing the child window i use this.close() on button click , but i want to close both parent and child window by click button of child window . 为了关闭子窗口,我在单击按钮时使用this.close(),但是我想通过单击子窗口的按钮来关闭父窗口和子窗口。

Try this 尝试这个

If Child.ShowDialog = DialogResult.OK Then
          Parent.close
        End If

Child form close button 子窗体关闭按钮

Me.diaglogresult=DialogResult.OK

将父窗体传递到子窗体中,然后在关闭的事件处理程序中调用其c​​lose方法。

    private void btnOpenForm_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
        frm2.Show();
        this.Hide();
    }


    private void frm2_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Close();
    }

You can do the close of the parent form from the child but to me that seems to break the encapsulation principle a little. 您可以从孩子那里关闭父窗体,但对我来说似乎有点违反封装原理。

An alternative is to subscribe to the child form's closed event from the parent and then within the parent you can respond to that - see Winform form closed event for an implementation. 另一种选择是从父级订阅子窗体的关闭事件,然后您可以在父级中对此做出响应-有关实现,请参见Winform窗体关闭事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM