[英]How to show a form again after hiding it?
I have two forms. I need to open a second form with a button.我有两个 forms。我需要用一个按钮打开第二个表单。 When I open form2 I hide form1.
当我打开 form2 时,我隐藏了 form1。 However when I try to show form1 again from form2 with a button it doesn't work.
但是,当我尝试使用按钮从 form2 再次显示 form1 时,它不起作用。 My form1 code is:
我的 form1 代码是:
Form2 form2 = new Form2();
form2.ShowDialog();
Inside form2 code:里面的form2代码:
Form1.ActiveForm.ShowDialog();
or或者
Form1.ActiveForm.Show();
or或者
form1.show(); (form1 doesn't exist in the current context)
doesn't work.不起作用。 I do not want to open a new form
我不想打开新表格
Form1 form1 = new Form1();
form1.ShowDialog();
I want show the form which I hided before.我想显示我之前隐藏的表格。 Alternatively I can minimize it to taskbar
或者我可以将它最小化到任务栏
this.WindowState = FormWindowState.Minimized;
and maximize it from form2 again.并再次从 form2 最大化它。
Form2.ActiveForm.WindowState = FormWindowState.Maximized;
however the way I am trying is again doesn't work.但是我尝试的方式再次不起作用。 What is wrong with these ways?
这些方式有什么问题?
You could try (on Form1 button click) 你可以试试(点击Form1按钮)
Hide();
Form2 form2 = new Form2();
form2.ShowDialog();
form2 = null;
Show();
or (it should work) 或(它应该工作)
Hide();
using (Form2 form2 = new Form2())
form2.ShowDialog();
Show();
保留Form1
的实例并将其用于Show
或Hide
。
You can access Form1 from Form2 throught the Owner property if you show form2 like this: 如果您显示如下所示的form2,则可以通过Owner属性从Form2访问Form1:
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 请注意,您不必使用ShowDialog导致隐藏,并且可以在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();
}
FormCollection frm = Application.OpenForms;
foreach(Form f in frm)
{
if(f.Name=="yourformname")
{
f.Show();
this.Close();
this.Dispose();
return;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.