郵便番号検索ページを表示するときに、現在のフォームを 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
}
}
これで、2 番目のフォームでそれを確認し、必要に応じてそのフォームを非表示/表示できます。
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() イベントから行う必要はありません。戻るボタンから行うこともできます。