Danny Beckett
Danny Beckett

Reputation: 20806

How can I hide some properties for this inherited label?

I have the following code. How can I hide the AutoEllipsis, Image, ImageAlign, ImageIndex, ImageKey, ImageList and TabIndex properties?

Also, how do I set the default size to 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);
    }
}

Upvotes: 0

Views: 282

Answers (1)

Bobson
Bobson

Reputation: 13706

When you inherit from something, your new class needs to be able to behave as if it were the base type (one aspect of polymorphism). That means it can't provide less functionality than the base. So there's no way to remove those properties, because otherwise it wouldn't be a Label.

What you might want to consider is "Composition" instead.

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

Upvotes: 3

Related Questions