[英]Interactions between 2 forms (hide/show)
I have two WinForm objects, form1
and form2
. 我有两个WinForm对象,即form1
和form2
。 What I want to do is: 我想做的是:
a. 一种。 When the user clicks a button on form1
, this will get hidden and a new instance of form2
will show up. 当用户单击form1
上的按钮时,该按钮将被隐藏,并且将显示新的form2
实例。 So I did: 所以我做了:
form1.Hide();
form2 new_session = new form2();
form2.Show();
b. b。 From form2
, I would like the user to be able to click a button to recover the originary form1
from which he has generated the form2
instance. 我希望用户可以从form2
单击按钮以恢复他从中生成form2
实例的原始form1
。 Basically, something like: 基本上,类似:
form2.Hide();
form1.Show();
Clearly, form1
does not exist in the current context. 显然, form1
在当前上下文中不存在。 The solution I know to work around this problem is to create a property of form2
where I assign the form1
, so that I can call it back again: 我知道要解决此问题的解决方案是创建一个form2
属性,在其中分配form1
,以便可以再次调用它:
form1 pMyForm;
public form1 myForm
{
get{return pMyForm;}
set{pMyForm = value;}
}
and before to show the form2
... 和之前显示form2
...
form2 new_session = new form2();
form2.myForm = this;
However, I feel like there's a more proper way of doing this. 但是,我觉得这里有一种更合适的方法。 I thought I could have found the form1
into the property ParentForm
of form2
, but I find a null
instead. 我以为可以在form2
ParentForm
属性中找到form1
,但是我找到了null
。 At the same time, I've tried to assign form1
to the property Parent
of form2
but I get the error Top-level control cannot be added to a control
. 同时,我尝试将form1
分配给form2
Parent
属性,但出现错误Top-level control cannot be added to a control
。
Do you have any suggestion on how to better handle this problem? 您对如何更好地解决此问题有任何建议吗?
Form2.Owner = this;
And 和
Form1 form = (Form1)this.Owner;
See my full answer here: how to exit from application on click of (red X ) button right top on winform 在这里查看我的完整答案: 如何单击Winform右上方的(红色X)按钮以退出应用程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.