访问父窗体 [英] Accessing the parent Form
问题描述
我知道,标题似乎傻了,也想不出更好的东西,很抱歉。
I know that the title might seem silly, couldn't think of something better, sorry.
我有2表格(C#),主要形式保持第二的一个实例。 有没有一种办法..可以访问到Form1(入口点)的运行实例,并从窗口2的实例?
I have 2 Forms (C#), the main-form holds an instance of the second. Is there a way to.. get access to the running instance of Form1 (The entry point) and to his properties from the instance of form2?
每个人都告诉我要学习OOP。 我做到了,很久很久以前,我还是不要弄不明白。
Everybody tells me to learn OOP. I did, a long long time ago, and I still don"t get it.
推荐答案
您可能实例化窗体2从内部Form1上。经过instanciating并在展示它,你可以设置窗体2的属性,它引用到Form1是这样的:
You probably instanciated Form2 from within Form1. After instanciating and BEFORE showing it you could set a property on Form2 that refers to Form1 like this:
Form2 f2 = new Form2();
f2.TheParent = this;
f2.Show();
当然,你必须到所在的当前属性添加到窗体2类是能够做到这一点。
Of course you have to add the TheParent property to the Form2 class to be able to do that.
警告:尽管有可能这样一个更好的解决方案可能是创建一个包含所有必需/共享数据的一个单独的对象ANDD的对象传递给每个窗体以类似的方式。这将$ P $变得过于再加pvent您code。
Warning: although it is possible this way a better solution might be to create a seperate object that holds all required/shared data andd pass that object to each form in a similar way. This will prevent your code from becoming too coupled.
这篇关于访问父窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!