简体   繁体   English

C#TabPage继承

[英]C# TabPage inheritance

I have two classes. 我有两节课。 First class has TabPage control. 头等舱具有TabPage控件。 I want to change layout of TabPage in Child class(Class B). 我想在子类(B类)中更改TabPage的布局。 For example how to add simple button to tabPage control in Child class? 例如如何在Child类的tabPage控件中添加简单按钮?

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

Change the TabPage to public 将TabPage更改为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; 首先,您需要在A类中公开TabPage,然后将要添加的控件添加到TabPage控件集合中。 in this example i have added a button to the TabPage you can add many more controls similarly. 在此示例中,我向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);
}

It really depends upon your situation, if you want to have TabPage accessible from base class too, make it public otherwise protected. 这实际上取决于您的情况,如果您也想从基类中访问TabPage,请将其公开,否则将受到保护。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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