user1260967
user1260967

Reputation: 99

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 .

Upvotes: 0

Views: 4130

Answers (4)

Vahid
Vahid

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

Prince Jea
Prince Jea

Reputation: 5680

Try this

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

Child form close button

Me.diaglogresult=DialogResult.OK

Upvotes: 2

kaj
kaj

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

sethcall
sethcall

Reputation: 2897

Pass in the parent form into the child form and call its close method in the closed event handler.

Upvotes: 1

Related Questions