阻止表单关闭?的WinForms

时间:2010-07-11 11:16:46

标签: c# winforms

我有一个启动formB的表单。我希望形成隐藏,直到formb关闭。可能有一个变化formb由formC和其他人打开,所以我只是不能创建一个新的表单。有没有办法启动formB,隐藏和阻止直到关闭?

2 个答案:

答案 0 :(得分:9)

这应该这样做。

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

答案 1 :(得分:0)

您可以使用OnActivate事件隐藏所有者和Dispose事件以显示所有者。即使不从另一种形式调用form_b,此解决方案仍然有效:

form_x中的代码:

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

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);
}
相关问题