当您显示邮政编码查找页面时,只需将当前表单传递给 Show() 方法。这将设置 Owner() 属性。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show(this); // pass in the owner
}
}
现在您可以在第二种形式中检查它并根据需要隐藏/显示该形式:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.Shown += new EventHandler(Form2_Shown);
this.FormClosed += new FormClosedEventHandler(Form2_FormClosed);
}
void Form2_Shown(object sender, EventArgs e)
{
if (this.Owner != null)
{
this.Owner.Hide();
}
}
void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
if (this.Owner != null)
{
this.Owner.Show();
}
}
}
这不必通过 FormClosed() 事件来完成,您也可以通过后退按钮来完成。