简体   繁体   中英

C# TabPage inheritance

I have two classes. First class has TabPage control. I want to change layout of TabPage in Child class(Class B). For example how to add simple button to tabPage control in Child class?

Class A
{
   TabPage a;
}
Class B : Class A
{
}

Change the TabPage to public

    class A
    {
        public TabPage a;
    } 
    class B : A
    {

    }

First you need to have TabPage in Class A to be public, then add the controls you want to add in your TabPage control collection; in this example i have added a button to the TabPage you can add many more controls similarly.

class A
{
    public TabPage a;
} 
class B : A
{
        //Create a control to add and set its properties
        Button btn = new Button();
        btn.Location = new Point(20, 20);
        btn.Size = new Size(120, 25);
        btn.Text = "My new Button";
        //Add the control to the Tabpage.
        a.Controls.Add(btn);
}

It really depends upon your situation, if you want to have TabPage accessible from base class too, make it public otherwise protected.

For protected

class A
{
    //Visible only to Inheriting class;
    protected TabPage a;
} 
class B : A
{
        //Create a control to add and set its properties
        Button btn = new Button();
        btn.Location = new Point(20, 20);
        btn.Size = new Size(120, 25);
        btn.Text = "My new Button";
        //Add the control to the Tabpage.
        a.Controls.Add(btn);
        //This will be visible to everybody
        public TabPage b= a;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM