【问题标题】:C# TabPage inheritanceC# TabPage 继承
【发布时间】:2010-12-22 12:24:28
【问题描述】:

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

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

【问题讨论】:

  • 不为您的“TabPage a”变量指定访问器将使其私有。这意味着它将无法在“B”类中访问。制作'TabPage a;'一个“受保护”,在您的 B 类中,您可以访问该变量。然后你可以使用'a.Controls.Add(new Button());为该控件添加一个按钮。

标签: c# .net inheritance tabpage


【解决方案1】:

将 TabPage 更改为公共

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

    }

【讨论】:

【解决方案2】:

首先你需要让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,请将其设为 public 否则受保护。

受保护

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

【讨论】:

  • 谢谢。将 TabPage 声明为受保护更好吗?
猜你喜欢
  • 1970-01-01
  • 2015-04-19
  • 2012-10-30
  • 2023-04-06
  • 2010-11-05
  • 2016-03-02
  • 1970-01-01
  • 2017-03-31
  • 2012-02-13
相关资源
最近更新 更多