简体   繁体   English

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

[英]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? 如何隐藏AutoEllipsisImageImageAlignImageIndexImageKeyImageListTabIndex属性?

Also, how do I set the default size to 50x50px? 另外,如何将默认尺寸设置为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);
    }
}

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 . 因此无法删除这些属性,因为否则它将不是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.
 }

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

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