views:

58

answers:

5

why can't i access the tabcontrol from within myclass.cs?

e.g.

tabcontrol is on 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 didnt work

thnks :)

A: 

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.

Dani
i've already done that, as i said in my question.
baeltazor
So ? what is the problem ? I did that on a test project and it works !
Dani
A: 

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.

Webleeuw
A: 

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.

BillW
That might become a little messy when you want to reference more controls than only the tabcontrol to myclass.cs
Webleeuw
@Webleeuw I'm just responding to the poster's question "as asked" : I predict you and I would agree that if "myclass" needed to interact (handle events from, modify, access properties in, allow an external "whatever" to set internal properties, etc.) with MANY controls on specific Form(s) (or with object(s) or code in external Forms or classes, class libraries, etc.) a different programming strategy would be called for. best,
BillW
A: 

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

A9S6
A: 

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.

Jon Seigel