如何隐藏此继承标签的某些属性?

时间:2013-01-29 16:08:26

标签: c# .net winforms c#-4.0 inheritance

我有以下代码。如何隐藏AutoEllipsisImageImageAlignImageIndexImageKeyImageListTabIndex属性?

另外,如何将默认大小设置为50x50px?

public class GradientBox : Label
{
    [DefaultValue(false), Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override bool AutoSize { get; set; }

    [DefaultValue(false), Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override string Text { get; set; }

    [DefaultValue(false), Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override ContentAlignment TextAlign { get; set; }

    // NullRef Exception if use { get; set; }
    [DefaultValue(false), Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override Font Font
    {
        get { return base.Font; }
        set { base.Font = value; }
    }

    public override BorderStyle BorderStyle
    {
        get { return BorderStyle.FixedSingle; }
        set { base.BorderStyle = value; }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(0, this.Height), this.ForeColor, this.BackColor), ClientRectangle);
    }
}

1 个答案:

答案 0 :(得分:3)

当你继承某些东西时,你的新类需要能够像基本类型(多态的一个方面)那样表现。这意味着它无法提供 less 功能而不是基础。所以没有办法删除这些属性,因为否则它不会是Label

你可能想要考虑的是“组合”。

public class GradientBox : Control
{
    private Label myLabel;
    public GradientBox()
    {
        myLabel = new Label;
        // Set your default values
    }


    public Font Font
    {
        get { return myLabel.Font; }
        set { myLabel.Font = value; }
    }
    // repeat to expose just the properties you want.
 }