C#TabPage继承

时间:2010-12-22 12:24:28

标签: c# .net inheritance tabpage

我有两节课。第一类有TabPage控件。我想在Child类(B类)中更改TabPage的布局。例如,如何在Child类中向tabPage控件添加简单按钮?

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

2 个答案:

答案 0 :(得分:3)

将TabPage更改为公开

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

    }

答案 1 :(得分:1)

首先,您需要将A类中的TabPage设置为公共,然后在TabPage控件集合中添加要添加的控件;在这个例子中,我已经为TabPage添加了一个按钮,您可以类似地添加更多控件。

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);
}

这实际上取决于您的情况,如果您想要从基类访问TabPage,请将其公开,否则受到保护。

受保护

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;
}