阻塞直到窗体关闭?Winforms

3

我有一个表单,可以启动formB。我希望在formb关闭之前,forma被隐藏起来。可能会有其他表单像formC一样打开formB,所以我不能只是创建一个新的表单。有没有一种方法可以启动formB,隐藏并阻止直到关闭?


你所描述的技术是以模态方式显示对话框。更多信息请参考 - http://msdn.microsoft.com/zh-cn/library/aa984358(VS.71).aspx - George Howarth
2个回答

9

这应该可以解决。

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

无法工作,主窗口会被另一个应用程序的窗口隐藏。 - Hans Passant
1
嗯,尝试调用ShowDialog()而不带参数。 - Jürgen Steinblock

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);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接