从 myclass.cs 访问 tabcontrol
为什么我无法从 myclass.cs 中访问 tabcontrol?
tabcontrol 位于 form1.cs 中,我尝试创建新标签页的代码位于 myclass.cs 中。我什至尝试将 tabcontrol 属性设置为公共,但这不起作用。
Why can't I access the tabcontrol from within myclass.cs?
The tabcontrol is in form1.cs and my code that tries to create a new tabpage is in myclass.cs. I've even tried setting tabcontrol property to public but that didn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需将控件上的修饰符更改为 public。
一旦你这样做了,你就得到了 Form myForm = new Form();
您将能够执行以下操作:
myForm.myTAB.TabPages.Add(myTabPage);
(当然,您需要创建 TabPage。
just change the modifier on the control to public.
Once you do that and you have Form myForm = new Form();
you will be able to do:
myForm.myTAB.TabPages.Add(myTabPage);
(You will need to create the TabPage of course.
我建议将 TabControl 包装在表单代码隐藏文件的公共只读属性中,而不是将控件本身公开。只是为了代码安全(比如能够将 TabControl 重新分配给新的东西)。
另外,不要忘记您需要 MyClass 中的表单实例,否则您将无法访问“MyTabControl”等实例成员,甚至无法访问公共实例字段(如果您选择这样做)。
I'd suggest to wrap the TabControl in an public readonly property on the form's codebehind file, rather than making the control itself public. Just for the sake of code security (like being able to reassign your TabControl to something new).
Also, don't forget you'll need an instance to your form in MyClass, otherwise you can't access instance members like "MyTabControl" or even public instance fields if you've chosen so.
我建议您在类 myclass.cs 中嵌入对表单上 TabControl 的引用。
您可以通过为 myclass 定义一个以 TabControl 作为参数的构造函数,或者通过在 myClass 中定义一个包含对 TabControl 的引用的公共属性来完成此操作。这里说明了两种方法:
因此,您可以使用 TabControl 在 Form 中以两种方式设置 TabControl 引用:
或者
另一种方法是在 Form1 上公开 TabControl 类型的公共属性:但是您必须考虑 myclass 将如何“查看“Form1 的当前“实例”...如果有多个 Form1 实例?如果存在唯一一个 TabControl,您可以使用 Form1 上的静态属性来公开它。
在这种情况下,“谁在创建谁”变得有趣:如果表单正在创建 myclass;如果 myclass 正在创建表单;如果 myclass 和 Form 都是由应用程序中的另一个实体创建的:我认为所有这些“向量”将决定应用的最佳技术是什么。
I suggest you embed a reference to the TabControl on the Form in the Class myclass.cs.
You could do this either by defining a constructor for myclass that took a TabControl as a parameter, or, by defining a Public Property in myClass that holds a reference to a TabControl. Both ways are illustrated here :
So you can set the TabControl reference in two ways from within the Form with the TabControl :
or
An alternative is to expose a Public Property of type TabControl on Form1 : but then you have to think about how myclass will "see" the current "instance" of Form1 ... if there is more than one instance of Form1 ? In the case there is one-and-only-one TabControl you could use a static Property on Form1 to expose it.
In this case "who is creating who" becomes of interest : if the Form is creating myclass; if myclass is creating the Form; if both myclass and the Form are being created by another entity in the application : I think all these "vectors" will bear on what is the best technique to apply.
您是否在 myclass.cs 中创建 form1 的实例并检查该实例上 tabControl 是否存在?如果你想访问 myclass.cs 中的 form1.tabControl 那么你需要在 form1.cs 中将 tabControl 设置为 public static
Are you creating an instance of form1 in myclass.cs and checking the existence of tabControl on that instance? If you want to access form1.tabControl in myclass.cs then you will need to make the tabControl as public static in form1.cs
这可能是一个过于简单的建议,您现在可能已经解决了这个问题,但是您是否记得在类文件的顶部包含
using System.Windows.Forms
?添加新的类文件时,Visual Studio 可能不会自动包含该引用。This might be an overly simple suggestion, and you've probably solved this by now, but did you remember to include
using System.Windows.Forms
at the top of the class file? Visual Studio may not include that reference automatically when adding a new class file.