Reputation: 99
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 .
Upvotes: 0
Views: 4130
Reputation: 364
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();
}
Upvotes: 0
Reputation: 5680
Try this
If Child.ShowDialog = DialogResult.OK Then
Parent.close
End If
Child form close button
Me.diaglogresult=DialogResult.OK
Upvotes: 2
Reputation: 5251
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.
Upvotes: 0
Reputation: 2897
Pass in the parent form into the child form and call its close method in the closed event handler.
Upvotes: 1