user34537
user34537

Reputation:

Block until a form closes? Winforms

I have a form that launches formB. I would like forma to be hidden until formb is closed. There may be a change formb is opened by formC and others so i simply cant just create a new form. Is there a way to launch formB, hide and block until close?

Upvotes: 3

Views: 4350

Answers (2)

Anax
Anax

Reputation: 9372

You can use the OnActivate event to hide the owner and the Dispose event to show the owner. This solution works even if form_b isn't called from another form:

Code in form_x:

FormB f = new FormB();
f.Show(this);

Code in form_b

this.Activated += new System.EventHandler(this.HideOwner);
private void HideOwner(object sender, EventArgs e)
{
    if (this.Owner != null) this.Owner.Hide();
}

protected override void Dispose(bool disposing)
{
    if (this.Owner != null) this.Owner.Show();
    if (disposing && (components != null))
    {
        components.Dispose();
    }
    base.Dispose(disposing);
}

Upvotes: 0

Jürgen Steinblock
Jürgen Steinblock

Reputation: 31723

This should do it.

this.Visible = false;
using (formB as new FormB())
    formB.ShowDialog(this);
this.Visible = true;

Upvotes: 9

Related Questions