开发者

accessing tabcontrol from myclass.cs

Why can't I access the tabcontrol from within myclass.cs?

The tabc开发者_如何学编程ontrol 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.


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.


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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜