从 myclass.cs 访问 tabcontrol

发布于 2024-08-13 01:30:55 字数 137 浏览 3 评论 0原文

为什么我无法从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烧了回忆取暖 2024-08-20 01:30:55

只需将控件上的修饰符更改为 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.

甜`诱少女 2024-08-20 01:30:55

我建议将 TabControl 包装在表单代码隐藏文件的公共只读属性中,而不是将控件本身公开。只是为了代码安全(比如能够将 TabControl 重新分配给新的东西)。

public TabControl MyTabControl {
  get {
    return privateTabControl;
  }
}

另外,不要忘记您需要 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).

public TabControl MyTabControl {
  get {
    return privateTabControl;
  }
}

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.

讽刺将军 2024-08-20 01:30:55

我建议您在类 myclass.cs 中嵌入对表单上 TabControl 的引用。

您可以通过为 myclass 定义一个以 TabControl 作为参数的构造函数,或者通过在 myClass 中定义一个包含对 TabControl 的引用的公共属性来完成此操作。这里说明了两种方法:

public class myclass
{
    // using "automatic" properties : requires C# 3.0
    public TabControl theTabControl { get; set; }

    // parameter-less 'ctor
    public myclass()
    {

    }

    // optional 'ctor where you pass in a reference to the TabControl
    public myclass(TabControl tbControl)
    {
        theTabControl = tbControl;
    }

    // an example method that would add a new TabPage to the TabControl
    public void addNewTabPage(string Title)
    {
        theTabControl.TabPages.Add(new TabPage(Title));
    }
}

因此,您可以使用 TabControl 在 Form 中以两种方式设置 TabControl 引用:

myclass myclassInstance = new myClass(this.tabControl1);

或者

myclass myclassInstance = new myClass();
// do some stuff
// now set the TabControl
myClassInstance.theTabControl = this.tabControl1;

另一种方法是在 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 :

public class myclass
{
    // using "automatic" properties : requires C# 3.0
    public TabControl theTabControl { get; set; }

    // parameter-less 'ctor
    public myclass()
    {

    }

    // optional 'ctor where you pass in a reference to the TabControl
    public myclass(TabControl tbControl)
    {
        theTabControl = tbControl;
    }

    // an example method that would add a new TabPage to the TabControl
    public void addNewTabPage(string Title)
    {
        theTabControl.TabPages.Add(new TabPage(Title));
    }
}

So you can set the TabControl reference in two ways from within the Form with the TabControl :

myclass myclassInstance = new myClass(this.tabControl1);

or

myclass myclassInstance = new myClass();
// do some stuff
// now set the TabControl
myClassInstance.theTabControl = this.tabControl1;

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.

沫离伤花 2024-08-20 01:30:55

您是否在 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

深海蓝天 2024-08-20 01:30:55

这可能是一个过于简单的建议,您现在可能已经解决了这个问题,但是您是否记得在类文件的顶部包含 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文