How to show a form again after hiding it?

笑着哭i 提交于 2019-11-30 15:21:40

You could try (on Form1 button click)

Hide();
Form2 form2 = new Form2();        
form2.ShowDialog();
form2 = null;
Show();

or (it should work)

Hide();
using (Form2 form2 = new Form2())       
    form2.ShowDialog();
Show();

Preserve the instance of Form1 and use it to Show or Hide.

You can access Form1 from Form2 throught the Owner property if you show form2 like this:

form2.ShowDialog( form1 )

or like this:

 form2.Show( form1 )

Notice this way you are not forced to use ShowDialog cause hide and show logic can be moved inside Form2

This method is the one that I find works the best for me

Primary Form

Form2 form2 = new Form2(this);

Secondary Form

private Form Form1
public Form2(Form Form1)
{
    InitializeComponent();
    this.Form1 = Form1;
    Form1.Hide();
}

Later on when closing

private void btnClose_Click(object sender, EventArgs e)
{
    Form1.Show();
    this.Close();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!