我有一个表单,可以启动formB。我希望在formb关闭之前,forma被隐藏起来。可能会有其他表单像formC一样打开formB,所以我不能只是创建一个新的表单。有没有一种方法可以启动formB,隐藏并阻止直到关闭?
这应该可以解决。
this.Visible = false;
using (formB as new FormB())
formB.ShowDialog(this);
this.Visible = true;
您可以使用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);
}